looping cell matching different sheet

bizztbiz

New Member
Joined
Nov 21, 2021
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
Hi all, I'm looking for a way to simplify the following step through looping. Appreciate any help to teach me simple steps for looping.
Basically, I just want to copy cells from one sheet, "Input" and paste the data into a sheet which name is written in cell (C5) in "Input"
Since I have 30 sheets, a loop will make it more simple and organize, written below is only for 3 sheets, "T-1", "T1" and "T2"
Actual sheets name range from "T-7" to "T23", but T0 does not exist.
Thank you for your help, I guess it will take only few minutes of your time.

VBA Code:
Sub DailyInput()

Sheets("Input").Select
   If Range("C5") = "T-1" Then
   Range("A5").Select
   Application.CutCopyMode = False
   Selection.Copy
Sheets("T-1").Range("A3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
Sheets("Input").Range("A7:EY1206").Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("T-1").Range("A5:EY1204").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
   
'----------------------------------------------------------------------------------------'
Sheets("Input").Select
   If Range("C5") = "T1" Then
   Range("A5").Select
   Application.CutCopyMode = False
   Selection.Copy
Sheets("T1").Range("A3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
Sheets("Input").Range("A7:EY1206").Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("T1").Range("A5:EY1204").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

'----------------------------------------------------------------------------------------'
Sheets("Input").Select
   If Range("C5") = "T2" Then
   Range("A5").Select
   Application.CutCopyMode = False
   Selection.Copy
Sheets("T2").Range("A3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
Sheets("Input").Range("A7:EY1206").Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("T2").Range("A5:EY1204").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

End Sub
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi & welcome to MrExcel.
How about
VBA Code:
Sub DailyInput()
   Dim ShtName As String
   
   With Sheets("Input")
      ShtName = .Range("C5").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
      Else
         MsgBox ShtName & " does not exist"
      End If
   End With
End Sub
 
Upvote 0
Solution
Sub DailyInput() Dim ShtName As String With Sheets("Input") ShtName = .Range("C5").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 Else MsgBox ShtName & " does not exist" End If End With End Sub
Thank you sir for your quick reply and it is working as intended...thank you again, really appreciate it.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Would you mind if I ask another question, I thought by looking at the example given by you I could figure out how to solve one more looping problem...
Thought it could be solve with For i = 1 to 3, Next i, something like that but apparently not...basic/fundamental understanding issue I guess on how to use command
My question is how do I integrate the PI Data Input and P2 Data Input into what you have written...
Refering to my previous question as an example, with sheets named either "T-1", "T1" or "T2" in cell (C5) in "Input"... and I have another sheet, named "Data History"

If cell (C5) in "Input" is "T-1", then
Range("CK7:CK206") in "Input" should be pasted to Range("CE4:CE203") in "Data History" for PI data
Range("CKL:CL206") in "Input" should be pasted to Range("DK4:CDK203") in "Data History" for PII data
But If cell (C5) in "Input" is "T1", then
Range("CK7:CK206") in "Input" should be pasted to Range("CF4:CF203") in "Data History" for PI data
Range("CKL:CL206") in "Input" should be pasted to Range("DL4:DL203") in "Data History" for PII data

I hope you can understand what am I try to show you and thanks in advance for helping me out on this...

Sub DailyInput()
Dim ShtName As String

With Sheets("Input")
ShtName = .Range("C5").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
Else
MsgBox ShtName & " does not exist"
End If
End With

'PI Data in Input
Sheets("Input").Select
Range("CK7:CK206").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data History").Range("CE4:CE203").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
'PII Data in Input
Sheets("Input").Select
Range("CL7:CL206").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Data History").Range("DK4:DK203").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

End Sub
 
Upvote 0
Sorry, not sure how to use the VBA code when writing....just highlight everything in the code and then press the VBA icon?
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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