Consulting

Results 1 to 6 of 6

Thread: Solved: Have code check for 2 sheet names and copy values from them

  1. #1
    VBAX Regular
    Joined
    Oct 2004
    Posts
    65
    Location

    Solved: Have code check for 2 sheet names and copy values from them

    Hi,

    I have the following VBA that’s Kind of working What I am looking for it to do is look through all open workbooks and Copy J5 only if the sheet name is "Alignment Score Summary" or "Score" then paste the values in the active sheet.

    Like I said it is sort of working but I am missing something and I am sure and I don’t know if I need the two loops or there is a better way to do this

    Any help is appreciated

    thanks a lot in advance

    [VBA]On Error Resume Next
    '''' new sheet
    For Each wbk In Workbooks
    If wbk.Name <> ThisWorkBook.Name Then
    '' wbk.Sheet1.Range("j5").Copy
    wbk.Sheets("Alignment Score Summary").Range("j5").Copy
    ThisWorkBook.Sheets(1).Range("A" & row).Select
    '' ThisWorkBook.Sheets(1).PasteSpecial xlPaseValues
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    row = row + 1
    End If
    Next

    '''' old sheet
    On Error Resume Next
    For Each wbk In Workbooks
    If wbk.Name <> ThisWorkBook.Name Then
    '' wbk.Sheet1.Range("j5").Copy
    wbk.Sheets("Score").Range("j5").Copy
    ThisWorkBook.Sheets(1).Range("A" & row).Select
    '' ThisWorkBook.Sheets(1).PasteSpecial xlPaseValues
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    row = row + 1
    End If
    Next
    End Sub[/VBA]
    We are living in a world today
    where lemonade is made from
    artificial flavoring and furniture polish
    is made from real lemons...
    Alfred E Newman

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Not tested:

    Option Explicit
     
    Sub exa()
    Dim wb As Workbook
    Dim wks As Worksheet
     
        On Error Resume Next
        For Each wb In Workbooks
     
            If Not wb.Name = ThisWorkbook.Name Then
     
                Set wks = wb.Worksheets("Alignment Score Summary")
                If Not wks Is Nothing Then
                    With Sheet1 ' ThisWorkbook.Worksheets(1)
                        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = wks.Range("J5").Value
                    End With
                    Set wks = Nothing
                End If
                Set wks = wb.Worksheets("Score")
                If Not wks Is Nothing Then
                    With Sheet1
                        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = wks.Range("J5").Value
                    End With
                    Set wks = Nothing
                End If
            End If
     
        Next
        On Error GoTo 0
    End Sub

  3. #3
    VBAX Regular
    Joined
    Oct 2004
    Posts
    65
    Location
    thank you very much I am sure it something simple but I can't seem to figure it out @ With Sheet1 in the line below
    it gives a Compile error

    Variable not defined



    [VBA] Set wks = wb.Worksheets("Alignment Score Summary")
    If Not wks Is Nothing Then
    With Sheet1 ' ThisWorkbook.Worksheets(1)
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = wks.Range("J5").Value
    [/VBA]


    Any adtional help will be much apriceated

    thanks!!!!
    We are living in a world today
    where lemonade is made from
    artificial flavoring and furniture polish
    is made from real lemons...
    Alfred E Newman

  4. #4
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    Hai Try This

    and i attached sample file

    Dim wb As Workbook
    Dim x As Integer
    
    x = 1
    
    For Each wb In Workbooks
    
        If wb.Name <> ThisWorkbook.Name Then
            
            For i = 1 To wb.Sheets.Count
            
                If wb.Sheets(i).Name = "Alignment Score Summary" Or wb.Sheets(i).Name = "Score" Then
                
                    ThisWorkbook.ActiveSheet.Cells(x, 1).Value = wb.Sheets(i).Range("j5").Value
                    x = x + 1
                
                End If
            
            Next i
        
        End If
    
    Next

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mduff
    thank you very much I am sure it something simple but I can't seem to figure it out @ With Sheet1 in the line below
    it gives a Compile error

    Variable not defined



    [vba] Set wks = wb.Worksheets("Alignment Score Summary")
    If Not wks Is Nothing Then
    With Sheet1 ' ThisWorkbook.Worksheets(1)
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = wks.Range("J5").Value
    [/vba]

    Any adtional help will be much apriceated

    thanks!!!!
    Sorry. You were using the worksheet's index, I just substituted with a sheet's codename for testing, but forgot to take it out. To fix:

    With ThisWorkbook.Worksheets(1)
    Mark

  6. #6
    VBAX Regular
    Joined
    Oct 2004
    Posts
    65
    Location
    thanks very much both options are working
    We are living in a world today
    where lemonade is made from
    artificial flavoring and furniture polish
    is made from real lemons...
    Alfred E Newman

Posting Permissions

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