1

I am attempting to insert data from Excel to a SQL datbase by means of VBA. I am using the following structure in Excel:

enter image description here

I am using the following code:

 Private Sub CommandButton1_Click()

 Dim i                 As Integer
 Dim p                 As Integer
 Dim product           As String
 Dim version           As String
 Dim opt               As String
 Dim visible           As String
 Excel.Worksheets("Blad2").Select

i = 3
Do Until IsEmpty(Cells(i, 1))

opt = ActiveSheet.Cells(i, 1)

    p = 3
    Do Until IsEmpty(Cells(1, p))

        product = ActiveSheet.Cells(1, p)
        version = ActiveSheet.Cells(2, p)
        visible = ActiveSheet.Cells(i, p)

        Debug.Print product & version & opt & visible

    p = p + 1
    Loop

i = i + 1
Loop

End Sub

The result of running the script is as follows:

  product#1  version#1   option#1   0
  product#1  version#2   option#1   1
  option#1

While I want it to result in:

  product#1  version#1   option#1   0
  product#1  version#2   option#1   1
  product#1  version#1   option#2   0
  product#1  version#2   option#2   0

Could someone help me out?

Vityata
  • 42,633
  • 8
  • 55
  • 100
user2237168
  • 305
  • 1
  • 3
  • 17

2 Answers2

1

This is something that should work for the input of this:

enter image description here

bringing this:

product1 version1 option1 0
product1 version2 option1 1
product1 version1 option2 0
product1 version2 option2 0

Option Explicit

Public Sub TestMe()

    Dim k           As Long
    Dim i           As Long
    Dim p           As Long
    Dim product     As String
    Dim version     As String
    Dim opt         As String
    Dim visible     As String

    With ActiveSheet
        i = 3
        Do Until IsEmpty(.Cells(i, 1))
        p = 3
        k = 0
            Do Until IsEmpty(.Cells(1, p))
                opt = .Cells(i, 1)
                product = .Cells(1, p)
                visible = .Cells(i, p)
                version = .Cells(2, 3 + k)
                k = k + 1

                Debug.Print product & " " & version & " " & opt & " " & visible
                p = p + 1
            Loop
            i = i + 1
        Loop
    End With
End Sub

In general, consider using better names for the variables and using Long instead of Integer.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Thank you Vityata for your answer. However, it is not exactly what I want. I edited my main question a bit. Also based on the comment of Tom. Hopefully the question makes more sense now and hopefully you are still willing to help me out – user2237168 Sep 04 '17 at 11:36
  • @user2237168 - if you can make it to a minimal working example (minimal working example stackoverflow), I will try. E.g., try to remove all the useless parts of your question and try to leave only the input or output, as far as it can be replicated easily. E.g., in my answer there is no connection string or complicated SQL queries. – Vityata Sep 04 '17 at 12:12
  • Dear Vityata. I now edited my main question and only included the necessary code and outputs. Sorry for me not being clear in the first place. I hope my main question now makes more sense. – user2237168 Sep 04 '17 at 12:50
  • @user2237168 - that is much better, see the edited answer :) – Vityata Sep 04 '17 at 13:04
  • 1
    Thanks for your help Vityata, the solution works like a charm! – user2237168 Sep 04 '17 at 13:53
1

I might be missing something but seems the variable visible is stucked with row 3 .Cells(3, p), that's why it only inserting Option 1 and disregarding the first loop.

try to change it with visible = ActiveSheet.Cells(i, p)

Edit: You said that is it not working, but seems when i tested it, I am getting the right result.

There's a possibility that when SQL string to be executed is the issue here.

enter image description here

kulapo
  • 397
  • 3
  • 15
  • Once again, thanks for your help Kulapo. I really appreciate it! I eventually used the solution from Vityata. – user2237168 Sep 04 '17 at 13:53
  • But you are indeed right. It is quite strange that the sql does not work. I observed that when adding the K (as in the solution form Vityata) the SQL does work and the data is inserted properly ... – user2237168 Sep 04 '17 at 14:32