Excel VBA question

zebehm

New Member
Joined
Mar 24, 2013
Messages
4
I'm trying to create a macro that will help me sort data. I take a table of information from a website and import it into a worksheet where I will draw information from it. I'd like the macro to have an if statement where depending on the value of a particular cell in the row it will copy values of a few cells from that row and place them in the right worksheet. Is that possible?


For instance:

If in worksheet1 cell A2 = 1234, copy cells D2, G2, H2 and I2 to worksheet2 starting in cell B3.

Thanks for the help.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Something like this.
Code:
Sub cpyCels()  'Example
Dim sh1 As Worksheet, sh2 As Worksheet, cy As Range
Set sh1 = Sheets(1)
Set sh2 = Sheets(2)
If sh1.Rangne("A2") = 1234 Then
 `With sh2
  Set cy = Union(.Range("D2"), .Range("G2:I2"))
  cy.Copy sh2.Ramge("B3")
  'Or, if you want it all posted in column B
  'cy.Copy
  'sh2.Range("B3").PasteSpecial Transpose:=True
 End With
End If
End Sub
 
Upvote 0
I'm trying to create a macro that will help me sort data. I take a table of information from a website and import it into a worksheet where I will draw information from it. I'd like the macro to have an if statement where depending on the value of a particular cell in the row it will copy values of a few cells from that row and place them in the right worksheet. Is that possible?


For instance:

If in worksheet1 cell A2 = 1234, copy cells D2, G2, H2 and I2 to worksheet2 starting in cell B3.

Thanks for the help.
These work a lot better without all the typos.
Code:
Sub cpyCels()  'Example
Dim sh1 As Worksheet, sh2 As Worksheet, cy As Range
Set sh1 = Sheets(1)
Set sh2 = Sheets(2)
If sh1.Range("A2") = 1234 Then
 With sh1
  Set cy = Union(.Range("D2"), .Range("G2:I2"))
  cy.Copy sh2.Range("B3")
  'Or, if you want it all posted in column B
  'cy.Copy
  'sh2.Range("B3").PasteSpecial Transpose:=True
 End With
End If
End Sub
 
Upvote 0
A few spelling errors in the code I fixed and got rid of the ` before the With sh2 and it runs, but there is no output when A2 = 1234, I tried deleting the ' before cy.Copy and sh2.Range("B3").PasteSpecial Transpose:=True but all that did was highlight the cells.

EDIT: Your updated code works great. Thanks!

New question: Is it possible to create a loop to go through about 20 rows to check to see if the first cell = 1234 and then copy the same cells as before to the second worksheet?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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