+ Reply to Thread
Results 1 to 9 of 9

Sheet Ref in VBA code reference to Cell?

  1. #1
    Forum Contributor
    Join Date
    09-12-2015
    Location
    Okinawa, Japan
    MS-Off Ver
    2013
    Posts
    178

    Sheet Ref in VBA code reference to Cell?

    I have a section of VBA code (thank you John Davis ) as follows:

    Sub majime01()
    Dim ws As Worksheet, x As String
    x = Sheets("Sheet1").Range("A1")
    For Each ws In Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6"))
    ws.Range("B" & Rows.Count).End(3)(2) = x
    Next ws
    End Sub

    Is it possible to have the Sheet Array reference back to another sheet's cells? For example:

    For Each ws In Sheets(Array("Sheet2CellA1", "Sheet2CellA2", "Sheet2CellA3", "Sheet2CellA4", "Sheet2CellA5"))

    This way the code could affect a variable of other sheets based on changing values on Sheet 2.

    Thank you in advance.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Sheet Ref in VBA code reference to Cell?

    Hi majime01,
    It looks you forgot the code tags, is it?
    PCI
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Sheet Ref in VBA code reference to Cell?

    Try next code
    note: see the use of the code tags
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    09-12-2015
    Location
    Okinawa, Japan
    MS-Off Ver
    2013
    Posts
    178

    Re: Sheet Ref in VBA code reference to Cell?

    Here are the two sets of code that run.

    Sub AddtoAllSheets()
    Dim ws As Worksheet, x As String
    x = Sheets("HomePage").Range("Q12")
    For Each ws In Sheets(Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"))
    ws.Range("B" & Rows.Count).End(3)(2) = x
    Next ws
    End Sub


    Sub majime02()
    Dim ws As Worksheet
    Dim x As String
    Dim WkRg
    x = Sheets("HomePage").Range("Q3")
    For Each WkRg In Array("Table Assignments!K17", "Table Assignments!K18", "Table Assignments!K17", "Table Assignments!K19", "Table Assignments!K20", "Table Assignments!K21")
    Range(WkRg) = x
    Next
    End Sub



    The first one works great and puts data in all 30 sheets as needed. The second one does not work. I basically need it to run just like the first one, but select workbook sheets based off of the number listed in sheet and cells"Table Assignments!K18" through K21.

    Possible or ??

    Thank you in advance.

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Sheet Ref in VBA code reference to Cell?

    Is it whet you want ...?
    See also the file attached

    PHP Code: 

    Sub majime03
    ()
    Dim ws As Worksheet
    Dim x 
    As String
    Dim WkRg
       x 
    Sheets("HomePage").Range("Q3")
       For 
    Each WkRg In Array("'Table Assignments'!K17""'Table Assignments'!K18""'Table Assignments'!K17""'Table Assignments'!K19""'Table Assignments'!K20""'Table Assignments'!K21")
          
    Sheets(CStr(Range(WkRg))).Range("B" Rows.Count).End(3)(2) = x
       Next
    End Sub 
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    09-12-2015
    Location
    Okinawa, Japan
    MS-Off Ver
    2013
    Posts
    178

    Re: Sheet Ref in VBA code reference to Cell?

    Thank you PCI, I like the way the code runs. But I do have a follow-up question if I may. I noticed that if there is no data in say 'Table Assignments'!K21, (a blank cell) then it throws an error. Is there a way to adjust for that or do you always need to match the number of worksheets in the array? Thank you in advance.

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Sheet Ref in VBA code reference to Cell?

    See next code
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    09-12-2015
    Location
    Okinawa, Japan
    MS-Off Ver
    2013
    Posts
    178

    Re: Sheet Ref in VBA code reference to Cell?

    PCI,

    Amazing job. Just what I was looking for. Well done sir.

    Thank you very much.

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,097

    Re: Sheet Ref in VBA code reference to Cell?

    You are welcome
    Enjoy Excel

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Correct Sheet Reference & Code not working?
    By liquidmettle in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-17-2015, 09:33 PM
  2. Indirect Sheet reference with cell reference left function and text
    By teststrip in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-01-2015, 10:31 AM
  3. [SOLVED] =INDIRECT, Sheet reference with Incremental cell reference
    By Deap in forum Excel General
    Replies: 4
    Last Post: 06-16-2014, 05:58 AM
  4. [SOLVED] Calling string inside cell reference to reference another sheet.
    By {=OR(value=array)} in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-26-2013, 04:45 PM
  5. Dragging formula, change sheet reference, but not cell reference
    By Kalilaya0419 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-13-2013, 04:50 PM
  6. VBA to hide sheet in workbook and also reference in code
    By rlsublime in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-21-2011, 07:20 PM
  7. Replies: 6
    Last Post: 10-31-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1