PC Review


Reply
Thread Tools Rate Thread

Copy data from one tab to another based on cell value on one tab

 
 
brentm
Guest
Posts: n/a
 
      30th Jun 2008
I have a spreadsheet where each tab represents 1 week. the tabs are named
based on the week ending date that tab covers - "June 25", "July 2", etc.
There is a master tab named "Invoice" with the period ending date residing in
cell C10. Is there a way I can have a macro go to the appropriate tab based
on the date entered in C10 on the "Invoice" tab? I need the macro to copy
over specific information, which I can do, but I wanted the macro to
automatically look at the value in C10 on "Invoice", then go get the
information from the appropriate tab that corrosponds to that cell value.
Anyone have any ideas?
 
Reply With Quote
 
 
 
 
Stephen Lloyd
Guest
Posts: n/a
 
      30th Jun 2008
I haven't tested this, but I think it will put you on the right track.

Sub FindWorksheet()

Dim wb As workbook
Dim wsInv As worksheet
Dim wslookup As worksheet
Dim rDate As range

Set wb = Activeworkbook
Set wsInv = wb.Worksheets("Invoice")
Set rDate = wsInv.Range("C10")

For Each wslookup in wb.Worksheets
If rDate.Value <= CDate(wslookup.Name) And rDate.Value >
CDate(wslookup.Name)-7 Then
'Enter Code to pull information
End If
Next wslookup

End Sub

"brentm" wrote:

> I have a spreadsheet where each tab represents 1 week. the tabs are named
> based on the week ending date that tab covers - "June 25", "July 2", etc.
> There is a master tab named "Invoice" with the period ending date residing in
> cell C10. Is there a way I can have a macro go to the appropriate tab based
> on the date entered in C10 on the "Invoice" tab? I need the macro to copy
> over specific information, which I can do, but I wanted the macro to
> automatically look at the value in C10 on "Invoice", then go get the
> information from the appropriate tab that corrosponds to that cell value.
> Anyone have any ideas?

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      30th Jun 2008
You will use a structure similar to this...

Dim WS As Worksheet
For Each WS In Worksheets
If WS.Name = Format(Range("C10").Value, "mmmm d") Then
'
' << Do whatever here >>
'
Exit For
End If
Next

Put the code you say you know how to do where indicated.

Rick


"brentm" <(E-Mail Removed)> wrote in message
news:839DDB59-6903-4923-AC44-(E-Mail Removed)...
>I have a spreadsheet where each tab represents 1 week. the tabs are named
> based on the week ending date that tab covers - "June 25", "July 2", etc.
> There is a master tab named "Invoice" with the period ending date residing
> in
> cell C10. Is there a way I can have a macro go to the appropriate tab
> based
> on the date entered in C10 on the "Invoice" tab? I need the macro to copy
> over specific information, which I can do, but I wanted the macro to
> automatically look at the value in C10 on "Invoice", then go get the
> information from the appropriate tab that corrosponds to that cell value.
> Anyone have any ideas?


 
Reply With Quote
 
brentm
Guest
Posts: n/a
 
      1st Jul 2008
Rick,

Thanks, but here is the code I have. When the macro is run, nothing
happens. I do not get any errors either. When I run to debug, there are no
problems found. What am I missing?

Sub Copy_SolutionB()
'
' Copy_SolutionB Macro
'

'
Dim WS As Worksheet
For Each WS In Worksheets
If WS.Name = Format(Range("G10").Value, "mmmm d") Then
Range("C2:E8").Select
Selection.Copy
Sheets("Invoice").Select
Range("C14:E20").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("C14:E14,C16:E16,C18:E18,C20:E20").Select
Range("C20").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
End If
Exit For
Next

End Sub

Thanks a ton for your expert help!
Brent


"Rick Rothstein (MVP - VB)" wrote:

> You will use a structure similar to this...
>
> Dim WS As Worksheet
> For Each WS In Worksheets
> If WS.Name = Format(Range("C10").Value, "mmmm d") Then
> '
> ' << Do whatever here >>
> '
> Exit For
> End If
> Next
>
> Put the code you say you know how to do where indicated.
>
> Rick
>
>
> "brentm" <(E-Mail Removed)> wrote in message
> news:839DDB59-6903-4923-AC44-(E-Mail Removed)...
> >I have a spreadsheet where each tab represents 1 week. the tabs are named
> > based on the week ending date that tab covers - "June 25", "July 2", etc.
> > There is a master tab named "Invoice" with the period ending date residing
> > in
> > cell C10. Is there a way I can have a macro go to the appropriate tab
> > based
> > on the date entered in C10 on the "Invoice" tab? I need the macro to copy
> > over specific information, which I can do, but I wanted the macro to
> > automatically look at the value in C10 on "Invoice", then go get the
> > information from the appropriate tab that corrosponds to that cell value.
> > Anyone have any ideas?

>
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      1st Jul 2008
That's a lot of code to go through. Before doing so, let me point out 2
things that could possibly cause trouble for you... write back if this
doesn't solve your problem.

1) In your first If-Then statement, you reference "G10" but in your original
posting you said "C10" contained the date you needed to compare against. Is
"G10" wrong?

2) You have several unqualified Range statements; I think qualifying them
would be helpful. For example, in the first If-Then statement referenced in
1) above, you should probably qualify the Range("G10").Value, C10 if the G10
is a mistype, with the worksheet it is on...

Worksheets("Invoice").Range("G10").Value

The first statement inside the above If-Then block is this...

Range("C2:E8").Select

Since the statement is inside the loop, I presume it should be referencing
the worksheet currently being iterated through. So that it doesn't reference
the active sheet and keep getting written over as the loop executes, you
should qualify it with the worksheet currently being iterated on...

WS.Range("C2:E8").Select

You should go through all your code and make sure each range is qualified as
to the worksheet it should be referencing.

Rick


"brentm" <(E-Mail Removed)> wrote in message
news:6EC77E86-58FE-4DAB-ACE8-(E-Mail Removed)...
> Rick,
>
> Thanks, but here is the code I have. When the macro is run, nothing
> happens. I do not get any errors either. When I run to debug, there are
> no
> problems found. What am I missing?
>
> Sub Copy_SolutionB()
> '
> ' Copy_SolutionB Macro
> '
>
> '
> Dim WS As Worksheet
> For Each WS In Worksheets
> If WS.Name = Format(Range("G10").Value, "mmmm d") Then
> Range("C2:E8").Select
> Selection.Copy
> Sheets("Invoice").Select
> Range("C14:E20").Select
> ActiveSheet.Paste
> Application.CutCopyMode = False
> Selection.Borders(xlDiagonalDown).LineStyle = xlNone
> Selection.Borders(xlDiagonalUp).LineStyle = xlNone
> With Selection.Borders(xlEdgeLeft)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlEdgeTop)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlEdgeBottom)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlEdgeRight)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlInsideVertical)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlInsideHorizontal)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> Selection.Borders(xlDiagonalDown).LineStyle = xlNone
> Selection.Borders(xlDiagonalUp).LineStyle = xlNone
> With Selection.Borders(xlEdgeLeft)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlEdgeTop)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlMedium
> End With
> With Selection.Borders(xlEdgeBottom)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlEdgeRight)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlInsideVertical)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> With Selection.Borders(xlInsideHorizontal)
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .TintAndShade = 0
> .Weight = xlThin
> End With
> Range("C14:E14,C16:E16,C18:E18,C20:E20").Select
> Range("C20").Activate
> With Selection.Interior
> .Pattern = xlSolid
> .PatternColorIndex = xlAutomatic
> .ThemeColor = xlThemeColorDark1
> .TintAndShade = -0.249977111117893
> .PatternTintAndShade = 0
> End With
> End If
> Exit For
> Next
>
> End Sub
>
> Thanks a ton for your expert help!
> Brent
>
>
> "Rick Rothstein (MVP - VB)" wrote:
>
>> You will use a structure similar to this...
>>
>> Dim WS As Worksheet
>> For Each WS In Worksheets
>> If WS.Name = Format(Range("C10").Value, "mmmm d") Then
>> '
>> ' << Do whatever here >>
>> '
>> Exit For
>> End If
>> Next
>>
>> Put the code you say you know how to do where indicated.
>>
>> Rick
>>
>>
>> "brentm" <(E-Mail Removed)> wrote in message
>> news:839DDB59-6903-4923-AC44-(E-Mail Removed)...
>> >I have a spreadsheet where each tab represents 1 week. the tabs are
>> >named
>> > based on the week ending date that tab covers - "June 25", "July 2",
>> > etc.
>> > There is a master tab named "Invoice" with the period ending date
>> > residing
>> > in
>> > cell C10. Is there a way I can have a macro go to the appropriate tab
>> > based
>> > on the date entered in C10 on the "Invoice" tab? I need the macro to
>> > copy
>> > over specific information, which I can do, but I wanted the macro to
>> > automatically look at the value in C10 on "Invoice", then go get the
>> > information from the appropriate tab that corrosponds to that cell
>> > value.
>> > Anyone have any ideas?

>>
>>


 
Reply With Quote
 
brentm
Guest
Posts: n/a
 
      1st Jul 2008
Rick,

Got it! Thanks for all your help.

Brent

"Rick Rothstein (MVP - VB)" wrote:

> That's a lot of code to go through. Before doing so, let me point out 2
> things that could possibly cause trouble for you... write back if this
> doesn't solve your problem.
>
> 1) In your first If-Then statement, you reference "G10" but in your original
> posting you said "C10" contained the date you needed to compare against. Is
> "G10" wrong?
>
> 2) You have several unqualified Range statements; I think qualifying them
> would be helpful. For example, in the first If-Then statement referenced in
> 1) above, you should probably qualify the Range("G10").Value, C10 if the G10
> is a mistype, with the worksheet it is on...
>
> Worksheets("Invoice").Range("G10").Value
>
> The first statement inside the above If-Then block is this...
>
> Range("C2:E8").Select
>
> Since the statement is inside the loop, I presume it should be referencing
> the worksheet currently being iterated through. So that it doesn't reference
> the active sheet and keep getting written over as the loop executes, you
> should qualify it with the worksheet currently being iterated on...
>
> WS.Range("C2:E8").Select
>
> You should go through all your code and make sure each range is qualified as
> to the worksheet it should be referencing.
>
> Rick
>
>
> "brentm" <(E-Mail Removed)> wrote in message
> news:6EC77E86-58FE-4DAB-ACE8-(E-Mail Removed)...
> > Rick,
> >
> > Thanks, but here is the code I have. When the macro is run, nothing
> > happens. I do not get any errors either. When I run to debug, there are
> > no
> > problems found. What am I missing?
> >
> > Sub Copy_SolutionB()
> > '
> > ' Copy_SolutionB Macro
> > '
> >
> > '
> > Dim WS As Worksheet
> > For Each WS In Worksheets
> > If WS.Name = Format(Range("G10").Value, "mmmm d") Then
> > Range("C2:E8").Select
> > Selection.Copy
> > Sheets("Invoice").Select
> > Range("C14:E20").Select
> > ActiveSheet.Paste
> > Application.CutCopyMode = False
> > Selection.Borders(xlDiagonalDown).LineStyle = xlNone
> > Selection.Borders(xlDiagonalUp).LineStyle = xlNone
> > With Selection.Borders(xlEdgeLeft)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlEdgeTop)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlEdgeBottom)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlEdgeRight)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlInsideVertical)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlInsideHorizontal)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > Selection.Borders(xlDiagonalDown).LineStyle = xlNone
> > Selection.Borders(xlDiagonalUp).LineStyle = xlNone
> > With Selection.Borders(xlEdgeLeft)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlEdgeTop)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlMedium
> > End With
> > With Selection.Borders(xlEdgeBottom)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlEdgeRight)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlInsideVertical)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > With Selection.Borders(xlInsideHorizontal)
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .TintAndShade = 0
> > .Weight = xlThin
> > End With
> > Range("C14:E14,C16:E16,C18:E18,C20:E20").Select
> > Range("C20").Activate
> > With Selection.Interior
> > .Pattern = xlSolid
> > .PatternColorIndex = xlAutomatic
> > .ThemeColor = xlThemeColorDark1
> > .TintAndShade = -0.249977111117893
> > .PatternTintAndShade = 0
> > End With
> > End If
> > Exit For
> > Next
> >
> > End Sub
> >
> > Thanks a ton for your expert help!
> > Brent
> >
> >
> > "Rick Rothstein (MVP - VB)" wrote:
> >
> >> You will use a structure similar to this...
> >>
> >> Dim WS As Worksheet
> >> For Each WS In Worksheets
> >> If WS.Name = Format(Range("C10").Value, "mmmm d") Then
> >> '
> >> ' << Do whatever here >>
> >> '
> >> Exit For
> >> End If
> >> Next
> >>
> >> Put the code you say you know how to do where indicated.
> >>
> >> Rick
> >>
> >>
> >> "brentm" <(E-Mail Removed)> wrote in message
> >> news:839DDB59-6903-4923-AC44-(E-Mail Removed)...
> >> >I have a spreadsheet where each tab represents 1 week. the tabs are
> >> >named
> >> > based on the week ending date that tab covers - "June 25", "July 2",
> >> > etc.
> >> > There is a master tab named "Invoice" with the period ending date
> >> > residing
> >> > in
> >> > cell C10. Is there a way I can have a macro go to the appropriate tab
> >> > based
> >> > on the date entered in C10 on the "Invoice" tab? I need the macro to
> >> > copy
> >> > over specific information, which I can do, but I wanted the macro to
> >> > automatically look at the value in C10 on "Invoice", then go get the
> >> > information from the appropriate tab that corrosponds to that cell
> >> > value.
> >> > Anyone have any ideas?
> >>
> >>

>
>

 
Reply With Quote
 
brentm
Guest
Posts: n/a
 
      1st Jul 2008
Stephen,

Thanks for your help. I went a different route, but your help got me
started. Thanks.

Brent

"Stephen Lloyd" wrote:

> I haven't tested this, but I think it will put you on the right track.
>
> Sub FindWorksheet()
>
> Dim wb As workbook
> Dim wsInv As worksheet
> Dim wslookup As worksheet
> Dim rDate As range
>
> Set wb = Activeworkbook
> Set wsInv = wb.Worksheets("Invoice")
> Set rDate = wsInv.Range("C10")
>
> For Each wslookup in wb.Worksheets
> If rDate.Value <= CDate(wslookup.Name) And rDate.Value >
> CDate(wslookup.Name)-7 Then
> 'Enter Code to pull information
> End If
> Next wslookup
>
> End Sub
>
> "brentm" wrote:
>
> > I have a spreadsheet where each tab represents 1 week. the tabs are named
> > based on the week ending date that tab covers - "June 25", "July 2", etc.
> > There is a master tab named "Invoice" with the period ending date residing in
> > cell C10. Is there a way I can have a macro go to the appropriate tab based
> > on the date entered in C10 on the "Invoice" tab? I need the macro to copy
> > over specific information, which I can do, but I wanted the macro to
> > automatically look at the value in C10 on "Invoice", then go get the
> > information from the appropriate tab that corrosponds to that cell value.
> > Anyone have any ideas?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy cell data from workbook based on user input =?Utf-8?B?TWljaGFlbCBB?= Microsoft Excel Programming 7 31st Dec 2005 03:07 PM
Copy data in column based on cell value oakman Microsoft Excel Programming 0 27th Oct 2004 05:49 PM
Copy data in column based on cell value oakman Microsoft Excel Programming 1 27th Oct 2004 05:13 PM
Copy data in column based on cell value oakman Microsoft Excel Programming 2 27th Oct 2004 04:00 PM
Macro to copy cell data to word document based on an active row? =?Utf-8?B?QnJpYW4=?= Microsoft Excel Programming 2 16th Sep 2004 01:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:07 AM.