Small adjustment to existing VBA copy macro

evenyougreg

New Member
Joined
Oct 1, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have this VBA code that used to work perfect, but I recently had to add a new column and so it needs to be adjusted, but no combination of adjustments I've attempted is correct.

Basically if there is a hostname match in A, I need to copy over K and L

But if there is a hostname match in A, and, B has the string "NONE" then I need to copy over H I J K L

Here's the code that I've been using:

VBA Code:
Sub copy_me_5000()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, fn As Range
Set sh1 = Workbooks("from_here.xlsx").Sheets("old")
Set sh2 = Workbooks("to_here.xlsx").Sheets("new")
    For Each c In sh1.Range("A2", sh1.Cells(Rows.Count, 1).End(xlUp))
        Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                If fn.Offset(, 1) = "NONE" Then c.Offset(, 4).Copy fn.Offset(, 4)
                c.Offset(, 8).Resize(, 4).Copy fn.Offset(, 8)
            End If
        Set fn = Nothing
    Next
End Sub


copy from here

from_here.xlsx
ABCDEFGHIJKLM
1hostnameminionVM OScheck1Grain OScheck2locationP/FStatusEnvOwnerNotes
2m-demo1m-demo1Microsoft Windows Server 2012 (64-bit)CMPTBLWindows-2012ServerR2CMPTBLvcenter1PASSINSTALLEDprod
3m-demo2m-demo2Microsoft Windows Server 2016 or later (64-bit)CMPTBLWindows-2016ServerCMPTBLvcenter1PASSINSTALLEDtest
4m-demo4m-demo4Microsoft Windows Server 2012 (64-bit)CMPTBLWindows-2012ServerR2CMPTBLvcenter1NOT MENOT MENOT MECOPY MECOPY ME
5m-demo5m-demo5Microsoft Windows Server 2016 or later (64-bit)CMPTBLWindows-2019ServerCMPTBLvcenter2PASSINSTALLEDdev
6l-demo1NONESUSE Linux Enterprise 11 (64-bit)UNSTBLunknownUNDFNDvcenter1FAILUNDEFINEDunknownNOT ME
7l-demo2NONESUSE Linux Enterprise 11 (64-bit)UNSTBLunknownUNDFNDvcenter1FAILUNDEFINEDunknown
8l-demo3NONEVMware Photon OS (64-bit)UNSTBLunknownUNDFNDvcenter2COPY MECOPY MECOPY MECOPY MECOPY ME
9l-demo4NONECentOS 4/5 or later (64-bit)UNDFNDunknownUNDFNDvcenter2FAILUNDEFINEDunknown
10l-demo5NONEUbuntu Linux (64-bit)CMPTBLunknownUNDFNDvcenter2FAILUNDEFINEDunknown
11l-demo6NONEUbuntu Linux (64-bit)CMPTBLunknownUNDFNDvcenter1FAILUNDEFINEDunknown
old


to here

to_here.xlsx
ABCDEFGHIJKLM
1hostnameminionVM OScheck1Grain OScheck2locationP/FStatusEnvOwnerNotes
2m-demo1m-demo1Microsoft Windows Server 2012 (64-bit)CMPTBLWindows-2012ServerR2CMPTBLvcenter1PASSINSTALLEDprod
3m-demo2m-demo2Microsoft Windows Server 2016 or later (64-bit)CMPTBLWindows-2016ServerCMPTBLvcenter1PASSINSTALLEDtest
4m-demo3m-demo3Microsoft Windows Server 2016 or later (64-bit)CMPTBLWindows-2016ServerCMPTBLvcenter2PASSINSTALLEDprod
5m-demo4m-demo4Microsoft Windows Server 2012 (64-bit)CMPTBLWindows-2012ServerR2CMPTBLvcenter1PASSINSTALLEDprod
6m-demo5m-demo5Microsoft Windows Server 2016 or later (64-bit)CMPTBLWindows-2019ServerCMPTBLvcenter2PASSINSTALLEDdev
7l-demo1NONESUSE Linux Enterprise 11 (64-bit)UNSTBLunknownUNDFNDvcenter1FAILUNDEFINEDunknown
8l-demo2NONESUSE Linux Enterprise 11 (64-bit)UNSTBLunknownUNDFNDvcenter1FAILUNDEFINEDunknown
9l-demo3NONEVMware Photon OS (64-bit)UNSTBLunknownUNDFNDvcenter2FAILUNDEFINEDunknown
10l-demo4NONECentOS 4/5 or later (64-bit)UNDFNDunknownUNDFNDvcenter2FAILUNDEFINEDunknown
11l-demo5NONEUbuntu Linux (64-bit)CMPTBLunknownUNDFNDvcenter2FAILUNDEFINEDunknown
Sheet1
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about
VBA Code:
Sub copy_me_5000()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, fn As Range
Set sh1 = Workbooks("from_here.xlsx").Sheets("old")
Set sh2 = Workbooks("to_here.xlsx").Sheets("new")
    For Each c In sh1.Range("A2", sh1.Cells(Rows.Count, 1).End(xlUp))
        Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                If fn.Offset(, 1) = "NONE" Then
                  c.Offset(, 7).Resize(, 5).Copy fn.Offset(, 7)
                Else
                  c.Offset(, 10).Resize(, 2).Copy fn.Offset(, 10)
                End If
            End If
        Set fn = Nothing
    Next
End Sub
 
Upvote 0
How about
VBA Code:
Sub copy_me_5000()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, fn As Range
Set sh1 = Workbooks("from_here.xlsx").Sheets("old")
Set sh2 = Workbooks("to_here.xlsx").Sheets("new")
    For Each c In sh1.Range("A2", sh1.Cells(Rows.Count, 1).End(xlUp))
        Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                If fn.Offset(, 1) = "NONE" Then
                  c.Offset(, 7).Resize(, 5).Copy fn.Offset(, 7)
                Else
                  c.Offset(, 10).Resize(, 2).Copy fn.Offset(, 10)
                End If
            End If
        Set fn = Nothing
    Next
End Sub

Thanks Fluff! You're a lifesaver
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
@Fluff I forgot I also need E copied over if the second condition is true:

"But if there is a hostname match in A, and, B has the string "NONE" then I need to copy over E H I J K L"

Sorry!
 
Upvote 0
Ok, how about
VBA Code:
Sub copy_me_5000()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, fn As Range
Set sh1 = Workbooks("from_here.xlsx").Sheets("old")
Set sh2 = Workbooks("to_here.xlsx").Sheets("new")
    For Each c In sh1.Range("A2", sh1.Cells(Rows.Count, 1).End(xlUp))
        Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
            If Not fn Is Nothing Then
                If fn.Offset(, 1) = "NONE" Then
                  c.Offset(, 4).Copy fn.Offset(, 4)
                  c.Offset(, 7).Resize(, 5).Copy fn.Offset(, 7)
                Else
                  c.Offset(, 10).Resize(, 2).Copy fn.Offset(, 10)
                End If
            End If
        Set fn = Nothing
    Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top