Copy multiple columns to cells (in another sheet) that match a criteria

bizztbiz

New Member
Joined
Nov 21, 2021
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
VBA and Excel experts,
Need help on this one...I'm looking for a looping method to simplify the below coding.
Multiple columns in "Input" should be copied to "Data History" columns that match the sheet's name in cell(B5) in "Input"
i.e If a sheet name in "Input" cell (B5) is "T-1", multiple columns in "Input" will be copied to multiple columns in "Data History" associated to "T-1" and according to the category, PI, PII and PIII
And for "T1" data the "Input" data will be pasted in the next column of "T-1" data in "Data History" according to the category PI,PII and PIII as well; please refer to below coding
Basically, the columns range in "Input" are fixed, but the data pasted in columns range in "Data History" are not.
Hopefully the explanation is clear, if not then please let me know.
Thanks in advance for your help.


VBA Code:
Sub DailyInput()
   Dim ShtName As String
   
With Sheets("Input")
    ShtName = .Range("B5").Value
    If Evaluate("isref('" & ShtName & "'!A1)") Then
        Sheets(ShtName).Range("A3").Value = .Range("A5").Value
        Sheets(ShtName).Range("A5:EY1204").Value = .Range("A7:EY1206").Value
If ShtName = "T-1" Then
Sheets("Data History").Range("CE4:CE203").Value = Range("CK7:CK206").Value 'PI
Sheets("Data History").Range("DK4:DK203").Value = Range("CL7:CL206").Value 'P2
Sheets("Data History").Range("EQ4:EQ203").Value = Range("CM7:CM206").Value 'P3
           
ElseIf ShtName = "T1" Then
Sheets("Data History").Range("CF4:CF203").Value = Range("CK7:CK206").Value 'PI
Sheets("Data History").Range("DL4:DL203").Value = Range("CL7:CL206").Value 'P2
Sheets("Data History").Range("ER4:ER203").Value = Range("CM7:CM206").Value 'P3
           
ElseIf ShtName = "T2" Then
Sheets("Data History").Range("CG4:CG203").Value = Range("CK7:CK206").Value 'PI
Sheets("Data History").Range("DM4:DM203").Value = Range("CL7:CL206").Value 'P2
Sheets("Data History").Range("ES4:ES203").Value = Range("CM7:CM206").Value 'P3

End If

Else
    MsgBox ShtName & " does not exist"
    End If
End With

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Update...I manage to loop one of the criteria but still has no idea how to loop the ShtName, I am not sure if this the correct way, but it gives me the result that I want but still it is a long code since I have actually 30 sheets in the loop, "T-7" to "T23"...
Any help on further simplifying the loop is very much appreciated, just for the sake of learning since I still get the results that I want through the long coding...
Thanks in advance guys...


VBA Code:
Sub TestDailyInput()
   Dim ShtName As String
   
With Sheets("Input")
    ShtName = .Range("B5").Value
    If Evaluate("isref('" & ShtName & "'!A1)") Then
        Sheets(ShtName).Range("A3").Value = .Range("A5").Value 'DATE INPUT
        Sheets(ShtName).Range("A5:EY1204").Value = .Range("A7:EY1206").Value 'DATA INPUT

If ShtName = "T-1" Then
For j = 1 To 18
i = 32 * j - 32
Sheets("Data History").Range("CE4:CE203").Offset(0, i).Value = Range("CK7:CK206").Offset(0, j - 1).Value 'T-7
Next j

ElseIf ShtName = "T1" Then
For j = 1 To 18
i = 32 * j - 32
Sheets("Data History").Range("CF4:CF203").Offset(0, i).Value = Range("CK7:CK206").Offset(0, j - 1).Value 'T-6
Next j

ElseIf ShtName = "T2" Then
For j = 1 To 18
i = 32 * j - 32
Sheets("Data History").Range("CG4:CG203").Offset(0, i).Value = Range("CK7:CK206").Offset(0, j - 1).Value 'T-5
Next j

End If

Else
    MsgBox ShtName & " does not exist"
    End If
End With

End Sub
 
Upvote 0
Declaration...the coding was actually written by Fluff when I asked in another thread, I just added the IfShtName="T-1" portion of the coding only
which explains how do I know to write the first loop for the sheet name but not for the second loop related to the ShtName :biggrin:
So thanks to Fluff for solving the first part of my problem, for the second part of the problem however, requires a new thread...

VBA Code:
Sub TestDailyInput()
   Dim ShtName As String
   
With Sheets("Input")
    ShtName = .Range("B5").Value
    If Evaluate("isref('" & ShtName & "'!A1)") Then
        Sheets(ShtName).Range("A3").Value = .Range("A5").Value 'DATE INPUT
        Sheets(ShtName).Range("A5:EY1204").Value = .Range("A7:EY1206").Value 'DATA INPUT

If ShtName = "T-1" Then
For j = 1 To 18
i = 32 * j - 32
Sheets("Data History").Range("CE4:CE203").Offset(0, i).Value = Range("CK7:CK206").Offset(0, j - 1).Value 'T-7
Next j

ElseIf ShtName = "T1" Then
For j = 1 To 18
i = 32 * j - 32
Sheets("Data History").Range("CF4:CF203").Offset(0, i).Value = Range("CK7:CK206").Offset(0, j - 1).Value 'T-6
Next j

ElseIf ShtName = "T2" Then
For j = 1 To 18
i = 32 * j - 32
Sheets("Data History").Range("CG4:CG203").Offset(0, i).Value = Range("CK7:CK206").Offset(0, j - 1).Value 'T-5
Next j

End If

Else
    MsgBox ShtName & " does not exist"
    End If
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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