VBA loop / do until

Adam Dyer

I'm trying to configure data in a very specific format in order for it to be uploaded to a software.

I have a count sheet that has the amount of lines needed to be entered on my upload template sheet. The values on the count sheet range between 3, 5, 7, and 9.

I want my VBA program to be able to enter values 1, 2, 3, (on 3 separate rows) on the upload template sheet if the value is "3" on the count sheet.

If the next value is "5" on the count sheet I want my program to be able to enter values 1, 2, 3, 2, 3 (on 5 separate rows on the upload sheet).

If a value is "9" on the count sheet, I want my program to enter values 1, 2, 3, 2, 3, 2, 3, 2, 3 (on 9 separate rows) and so on.

The first three values will always be 1, 2, 3, consecutively and then alternating between 2 and 3 based on the value on the count sheet.

Can anyone help me out here?

Toddleson

There aren't enough details to really write anything. But here's a tutorial/example of how to access data from seperate workbooks and how to write values into cells inside a loop.

The equation you were looking for where the output row # follows the pattern 1,2,3,2,3,2,3... is a little complicated with normal math, but it can be accomplished in programming math by using a Boolean expression as part of the equation.

Sub RowNumberer()
    Dim CountWB As Workbook
    Set CountWB = Workbooks("Your Count Sheet Workbook's Name (plus file extension)")
    
    Dim CountSheet As Worksheet
    Set CountSheet = CountWB.Worksheets("Your Count Sheet's Name")
    
    Dim CountSheetValue As Long
    CountSheetValue = CountSheet.Range("Cell Address of your Count result (3, 5, 7, 9)")
    
    Dim UploadWB As Workbook
    Set UploadWB = Workbooks("Your Upload Template Workbook's Name (plus file extension)")
    
    Dim OutputSheet As Worksheet
    Set OutputSheet = UploadWB.Sheets("The Name of the Sheet you want to number the rows on")
    
    Dim i As Long
    For i = 1 To CountSheetValue
        OutputSheet.Cells(i, 1) = i Mod 2 - 2 * (i > 1)
    Next i
End Sub

To explain the formula: i Mod 2 - 2 * (i > 1)

i Mod 2 is taking the iterating value i and retrieving the remainder after dividing by 2. This alternates between 1 and 0.

We would just need +2 in order to move that from 1 and 0 to 3 and 2. But we only want +2 after the first number since we want the first number to be 1.

So I added the boolean expression (i > 1) which is 0 when False and -1 when True. I don't know why True = -1 in VBA. So the term - 2 * (i > 1) is 0 when i = 1 and +2 when i > 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive