My VBA code is not working, please help me

Yashraj Jadhao

New Member
Joined
Nov 14, 2021
Messages
9
Office Version
  1. 2021
Platform
  1. MacOS
I have solved this question but my code is not working which I have written below, please help me with correct VBA code.
Question: Label the spreadsheet as shown below.
Then write a macro named “MultiplicationTable” that: (1) reads the number typed in by the user
into cell B2 and stores in a variable named “number1”, (2) reads the number typed in by the user
into cell B3 and stores in a variable named “number2”, (3) uses a FOR loop structure to print a
column of numbers starting at a value of 1 and going all the way to the value of variable number1
where the first entry is in cell A6 (and subsequent values are in A7, A8, etc.), (4) uses a FOR loop
structure to print a row of numbers starting at a value of 1 and going all the way to the value of
variable number2 where the first entry is in cell B5 (and subsequent entries are in C5, D5, etc.),
and (5) use a loop structure to then print in the multiplication table where the result in each cell
starting in cell B6 and going down both in the columns and across in the rows is the product of
the number labels in column A multipled by the label value in Row 5.
ABC
1
Multiplication Table Generator
2User Input Number #1 =
3
User Input Number #2 =
4
5X
6


ANSWER: This is my code but for some reason it is not working, please help.

Sub Button1_Click()
'Comment-Declaration of variables
Dim number1 As Integer
Dim number2 As Integer
'Comment-Defining varaibles
number1 = Int(Range("B2").Value)
number2 = Int(Range("B3").Value)
Dim cellidex As String
For i = 1 To number1
'Comment- Setting row index
cellindex = "A" & (i + 5)
'Comment-Setting. values with correspondence to index
Range(cellindex).Value = i
Next i
'Comment- Column header
For i = 1 To number1
For j = 1 To number2
cellindex = Chr(Asc("A" + i)) & 5
'Comment- Setting values with correspondence to index
Range(cellindex).Value = i
Next i
For i = 1 To number1
For j = 1 To number2
'Comment- Total cellindex
cellindex = Chr(Asc("A") + j) & (5 + i)
'Comment- Setting up multiplication values
Range(cellindex).Value = i * j
Next j
Next i
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Also, adding a value to an ASCII value will likely only get you up to 'Z' which should be fine, but above like 25 as an input, things will start having issues.

And what is in the original post that I edited.
 
Upvote 0
Also, adding a value to an ASCII value will likely only get you up to 'Z' which should be fine, but above like 25 as an input, things will start having issues.

And what is in the original post that I edited.
I am new to coding so I made this code from my notes, can you help me with easier code please
 
Upvote 0
From the wording of your question, it looks like this is some sort of homework or assignment. As such, we should really not do this for you (as that would not be ethical). However, we might be able to offer you some tips, such as using Cells(row, column) instead of Range(...) in your range refences, as you can use letters of numbers for your column reference in Cells.
 
Upvote 0
From the wording of your question, it looks like this is some sort of homework or assignment. As such, we should really not do this for you (as that would not be ethical). However, we might be able to offer you some tips, such as using Cells(row, column) instead of Range(...) in your range refences, as you can use letters of numbers for your column reference in Cells.
I already have my code, I am just asking for any alternative code just for my knowledge
 
Upvote 0
As an example rather than
VBA Code:
Dim cellidex As String
For i = 1 To number1
'Comment- Setting row index
cellindex = "A" & (i + 5)
'Comment-Setting. values with correspondence to index
Range(cellindex).Value = i
Next i
try
VBA Code:
For i = 1 To number1
    Cells(i + 5, "A").Value = i
Next
 
Upvote 0

Forum statistics

Threads
1,223,947
Messages
6,175,559
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top