+ Reply to Thread
Results 1 to 2 of 2

VBA variable declaration and assignment help

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2017
    Location
    California, US
    MS-Off Ver
    Office 2016
    Posts
    38

    VBA variable declaration and assignment help

    I am having trouble with the code in red. I would like to first search for the first empty cell in column C on Sheet1 then search on another sheet for a specific value to move over to the first empty cell in column C in sheet1.

    Option Explicit
    
    Sub Open_My_Files()
    Dim MyFile As String        ' File to be appeneded
    Dim MyPath As String        ' Pathname to where CSV files are kept
    Dim shS As Worksheet        ' Source worksheet
    Dim shD As Worksheet        ' Destination worksheet
    Dim lRow As Long
    Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
    Dim currentRowValue As String
    
    
    
    Application.DisplayAlerts = False
    
    ' Initialize Variable
    MyPath = "C:\\users\anthony777\desktop\"
    MyFile = Dir(MyPath & "\*.csv")
    
    
    ' Loop through files
    Do While MyFile <> ""
        ' Add a worksheet at the end of the current workbook and name it for the CSV fiel
        Worksheets.Add After:=Sheets(Sheets.Count)
        Set shD = Sheets(Sheets.Count)
        shD.Name = Replace(MyFile, ".csv", "")
        
        ' Open the CSV file
        Workbooks.Open MyPath & "\" & MyFile
        ' We can "get away with the following because we know the CSV file is active since we just opened it.
        ' We also know that CSV files only have one sheet
        Set shS = ActiveWorkbook.Sheets(1)
        
    
       
        ' Copy all on this sheet
        shS.Cells.Copy
        
        ' close the workbook
        ActiveWorkbook.Close savechanges:=False
        
        ' The book knows that shD is part of it.
        shD.Range("A1").PasteSpecial xlAll
        'activate correct sheet
        
        Worksheets("Inspection Data Sheet").Activate
        'find the first blank cell in column c
        sourceCol = 3
        rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
        currentRowValue = Cells(currentRow, sourceCol).Value
            If IsEmpty(currentRowValue) Or currentRowValue = "" Then
                Cells(currentRow, sourceCol).Select
    
            shS.Range("A1").Select
            Cells.Find(What:="axis", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
                :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False).Activate
            ActiveCell.Offset(1, 0).Select
            End If
            
            If ActiveCell = "TP" Then
                ActiveCell.Offset(0, 3).Select
            End If
            
            
    
    
       
        ' Get the next file
        MyFile = Dir()
    Loop
    Application.DisplayAlerts = True
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA variable declaration and asignment help

    Sub Open_My_Files()
        Dim MyFile    As String    ' File to be appeneded
        Dim MyPath    As String    ' Pathname to where CSV files are kept
        Dim shIDS     As Worksheet ' Inspection Data Sheet
        Dim shCSV     As Worksheet ' CSV worksheet
        Dim rngAxis   As Range
        Dim rngNext   As Range
        
        Application.DisplayAlerts = False
        
        ' Initialize Variable
        Set shIDS = Worksheets("Inspection Data Sheet")
        MyPath = "C:\\users\amirfar1\desktop\"
        MyFile = Dir(MyPath & "*.csv")
        ' With this synatax, you are telling Dir to get files that match *.csv - no need for an if statement later
        
        ' Loop through files
        Do While MyFile <> ""
        
            ' Open the CSV file
            With Workbooks.Open(MyPath & "\" & MyFile)
                ' We can "get away with the following because we know the CSV file is active since we just opened it.
                ' We also know that CSV files only have one sheet
                ' Copy sheet to this workbook
                .Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
                ' close the workbook
                .Close SaveChanges:=False
            End With
        
            Set shCSV = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            shCSV.Name = Replace(MyFile, ".csv", "")
        
            Set rngAxis = shCSV.Cells.Find(What:="axis", _
                                           LookIn:=xlFormulas, _
                                           LookAt:=xlPart, _
                                           SearchOrder:=xlByRows, _
                                           SearchDirection:=xlNext, _
                                           MatchCase:=False, _
                                           SearchFormat:=False)
        
            If Not rngAxis Is Nothing Then                                      'Test if axis found
                If UCase(rngAxis.Offset(1, 0).Value) = "TP" Then            'Test if TP found below axis
                    Set rngNext = shIDS.Range("C1:C32, C41:C67, C77:C103, C113:C139").Find("", , xlValues, , 1, 1)  'Find next empty cell
                    If Not rngNext Is Nothing Then
                        rngNext.Value = rngAxis.Offset(1, 3).Value     'copy value to empty cell.
                    Else
                        'No more empty cells in range
                    End If
                Else
                    'No TP found after axis
                End If
            Else
                'Axis not found
            End If
        
            ' Get the next file
            MyFile = Dir()
        Loop
        
        Application.DisplayAlerts = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Meaning of characters in %, !, $ and # in variable declaration
    By Plotseling in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-22-2013, 04:59 AM
  2. [SOLVED] Conditional public variable declaration
    By RedWing in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-13-2012, 11:16 AM
  3. [SOLVED] Variable Declaration issue
    By NewToCode in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2012, 01:57 PM
  4. passing a variable to an array declaration
    By labogola in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-09-2012, 04:10 AM
  5. Range Declaration using Variable
    By wfire in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2009, 11:43 AM
  6. Changing variable value without explicit declaration
    By carg1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2007, 02:42 PM
  7. A disturbing behaviour of DIM (for variable declaration)
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-11-2005, 01:20 PM
  8. Declaration of public variable - Question
    By Les Stout in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1