You are here:

Excel/Copy datas from Sheet1 to Sheet2 Range Wise

Advertisement


Question
Apologies for posting this again.

I had 1 more question but was unable to ask the same as I was unable to find the "Follow-up" tab. When clicked on the link I got your new code but only the tab "Rate the Expert" was visible.

Code -

Private Sub CommandButton1_Click()
Dim ans As Long, i As Long
Dim rw2 As Long
Dim r As Range
Dim sCol1 As String, sCol2 As String
Dim s1 As Variant, s2 As Variant
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
' Assumes column A in sheet2 will always have a value in column
' A of the last used row
rw2 = sh2.Cells(sh2.Rows.Count, "A").End(xlUp).Row + 1

ans = Application.InputBox("Enter Row Number", Type:=1)
If ans < 1 Or ans > Rows.Count Then
MsgBox "Invalid row: " & ans
Exit Sub
End If
Set r = Sheets("Sheet1").Range("F" & ans)
If r.Value = "AAAAA" Or r.Value = "BBBBB" Then
s1 = Split("B,C,D,E,F,H,I,J,K,M", ",")
s2 = Split("H,F,P,A,D,I,J,L,B,E", ",")
Else
s1 = Split("A,B,C,D,E,F,G,K,L,M", ",")
s2 = Split("O,H,F,P,A,D,K,B,G,E", ",")
End If
For i = LBound(s1) To UBound(s1)
  sCol1 = s1(i)
  sCol2 = s2(i)
  sh1.Range(sCol1 & ans).Copy sh2.Range(sCol2 & rw2)
Next

End Sub

I also need Range N in Sheet2 to get updated with value 1 each time when the macro is run.

Answer
Bimmy,


Private Sub CommandButton1_Click()
Dim ans As Long, i As Long
Dim rw2 As Long
Dim r As Range
Dim sCol1 As String, sCol2 As String
Dim s1 As Variant, s2 As Variant
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
' Assumes column A in sheet2 will always have a value in column
' A of the last used row
rw2 = sh2.Cells(sh2.Rows.Count, "A").End(xlUp).Row + 1

ans = Application.InputBox("Enter Row Number", Type:=1)
If ans < 1 Or ans > Rows.Count Then
MsgBox "Invalid row: " & ans
Exit Sub
End If
Set r = Sheets("Sheet1").Range("F" & ans)
If r.Value = "AAAAA" Or r.Value = "BBBBB" Then
s1 = Split("B,C,D,E,F,H,I,J,K,M", ",")
s2 = Split("H,F,P,A,D,I,J,L,B,E", ",")
Else
s1 = Split("A,B,C,D,E,F,G,K,L,M", ",")
s2 = Split("O,H,F,P,A,D,K,B,G,E", ",")
End If
For i = LBound(s1) To UBound(s1)
  sCol1 = s1(i)
  sCol2 = s2(i)
  sh1.Range(sCol1 & ans).Copy sh2.Range(sCol2 & rw2)
Next
sh2.Range("N" & rw2).value = 1   'added line

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.