Consulting

Results 1 to 10 of 10

Thread: Counter in Range

  1. #1
    Banned VBAX Newbie
    Joined
    Apr 2012
    Posts
    4
    Location

    Counter in Range

    Dear experts,

    I have the code below:

    Dim r As Range
     Dim i As Integer
     i = 15
    Set r = wb.Sheets("List of Products").Range("B7:B100")
            For Each cell In r
       If Not IsEmpty(cell) Then
          ' I update the current document with data from wb document
          .Range(E1 & i).Formula = wb.Worksheets("List of Products").Range("B7").Value
          Cancel = True
       End If

    As you can see I am trying to increment the cell number:
    Range(E & i) should be i.e. Range(E15)

    I tryed using Range(cell) but did not worked.

    In any other language I would of done:E(counter++)

    How can I use this counter in VBA ?

    Regards,

    Dragos
    Last edited by Aussiebear; 04-13-2023 at 01:53 PM. Reason: Adjusted the code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Dim r As Range 
    Dim i As Integer 
    i = 15 
    Set r = wb.Sheets("List of Products").Range("B7:B100") 
    For Each cell In r 
       If Not IsEmpty(cell) Then 
          ' I update the current document with data from wb document
          .Cells(i, "E").Formula = wb.Worksheets("List of Products").Range("B7").Value 
          i = i + 1
          Cancel = True 
       End If
    Last edited by Aussiebear; 04-13-2023 at 01:48 PM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Banned VBAX Newbie
    Joined
    Apr 2012
    Posts
    4
    Location
    Thank you xld!!

    I tryed similar for j, "B" but it throws an error:

    .Cells(i, "E").Formula = wb.Worksheets("List of Products").Range(j, "B").Value

    What is the trick there?
    Last edited by Aussiebear; 04-13-2023 at 01:49 PM. Reason: Adjusted the code tags

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    What does j represent?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Banned VBAX Newbie
    Joined
    Apr 2012
    Posts
    4
    Location
    Quote Originally Posted by Aussiebear
    What does j represent?
    Dim i, j as Integer
    I am trying to update the date in the current file row i with date from another file row j.


    Dim r As Range
            Dim i, j As Integer
            i = 15
            j = 7
    Set r = wb.Sheets("List of Products").Range("B7:B100")
            For Each cell In r
       If Not IsEmpty(cell) Then
          ' I update the current document with data from wb document
          .Cells(i, "E").Formula = wb.Worksheets("List of Products").Range("B7").Value
          .Cells(i, "P").Formula = wb.Worksheets("List of Products").Range("I7").Value ' here I need (j, "I")
           i = i + 1
          j = j + 1
          Cancel = True
            End If
    Last edited by Aussiebear; 04-13-2023 at 01:50 PM. Reason: Adjusted the code tags

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You don't use

    Range(j, "B")
    but

    Cells(j, "B")
    Last edited by Aussiebear; 04-13-2023 at 01:51 PM. Reason: Added code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    or
    Range("I" & j)
    Last edited by Aussiebear; 04-13-2023 at 01:51 PM. Reason: Added code tags
    ------------------------------------------------
    Happy Coding my friends

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by CatDaddy
    or Range("I" & j)
    I don't know why, but I just can't get to like that format
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    to each his own!
    ------------------------------------------------
    Happy Coding my friends

  10. #10
    Banned VBAX Newbie
    Joined
    Apr 2012
    Posts
    4
    Location
    It works!

    Thank you xld, CatDaddy and AussieBear!

Posting Permissions

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