Using VBA To Create VBA Code On The Fly

May 17, 2006

I have a vba module that reads a value from a txt file and saves that value in a variable called "iniInfo". I want to permanently assign that value programatically by using VBA code to create a Public variable e.g. "PUBLIC CONST conFIg = iniInfo" in my modGlobalVar Module. (Of course, this would only create the code if the "conFig" variable doesn't already exist.)

View 2 Replies


ADVERTISEMENT

Create Macro Code With VBA Code

Feb 22, 2008

I'm trying to add buttons to an excel spreadsheet at runtime. Each row in the spreadsheet should have its own buttons. I was able to create and edit them with the ActiveSheet. OLEObjects.Add() function, but after that, when i was trying to create code dynamically to react on the buttons' click events excel crashes (actually it works for one button, but not if my routine for adding a new button and event code is called more than once in a row!)

The code below works if the AddCmdbuttonWithCode() is called once, but crashes if it is called two or more times. Excel tries to restore the document after the crash and the first button and its corresponding click event code is visible and works... but NOT the second button and its event code...

The only way I can create multiple buttons right now is by calling my method once per click, opening the vba editor, changing the parameters for my AddCmdbuttonWithCode() routine and execute it again. After that I have mutliple buttons in different lines which all work fine (so the concept seems to work).

The problem seems to be the insertLine method, since everything seems to work if i leave it out (except for the code generation of course, since this is done by this part of the code :-) ). Is it possible that calling the insertLine Method can't be called multiple times? I don't know... any ideas? Feel free to test my code - it's small, easy to understand and has comments.

'this code calls the method which creates the buttons in specific cells with specific names
Private Sub CommandButton3_Click()
'the first call always works!
AddCmdbuttonWithCode "Edit_111_111_114", 23

'the second one crashes excel
AddCmdbuttonWithCode "Edit_111_111_115", 27
End Sub

View 3 Replies View Related

Create Search Code In VBA?

Mar 6, 2014

Create a search engine in vba for a worksheet, a macro to be assigned to a button. The 'search engine' to that 'Find button' that I have in mind is on these parameters:

- search any result, complete or partial, non-case sensitive;
- the text to search is typed in B2;
- after the text to search is typed, it only works by pressing that 'Find button';
- it only searches column D in the current worksheet;
- tells the total number of matches found in C2;
- when that Find button is clicked, it goes automatically to the first result found and stops;
- if that Find button is clicked again then it goes to the next found match and stops, and so on;
- when it reaches the end of the document, it just restarts to search from the beginning;
- if there are no matches, it just makes a sound and does nothing; and types zero (0) in C2.

View 11 Replies View Related

How To Create A Unique Code

May 7, 2008

Using a formula,

How can i get excel to create a custom unique number using the first three (or two) characters from another field and then adding four unique numbers to the characters.

basically heres the info

Field H2 contains the characters "ACS" so i would want another field to create a unique code to include something like "ACS1234" and to continue to the next row would be the first three characters of field H3 then 1235.

View 9 Replies View Related

Create Comparison Code In VBA

Jan 5, 2010

I have to do monthly comparisons of two excel worksheets, one being a vessel timesheet and the other being an excel download from manpower software that we use. The comparison that is being made are days of the month, whilst the unique identifier between the two sheets (To make it easier to understand I will call them sheet1 and sheet2) is an employee ID. Both sheets, alongside the name and employee ID, list all the days of the month that employees have worked. If they have worked, there is a "1" under that day, if they have not the cell is blank.

What I currently do manually is:

1: Search for the employee ID (and/or name) in sheet1 and compare the days with the employee ID in sheet2.
2: Highlight the differences
3: Move onto the next employee ID

All differences are highlighted in sheet2. Differences highlighted are in the days only, where if any days are missing/extra in either sheet, then the particular cell(s) are highlighted in yellow. If an employee is missing from sheet1 but is in sheet2, then all the days are highlighted (in sheet2). If an employee is missing from sheet2 but is in sheet1, then that particular employee is copied to the bottom of sheet2 (after last row).

As this is horribly complicated to explain, I can attach an example if this is sounding rather inexplicable? Is it possible to attach a xls to a thread?

View 9 Replies View Related

Code To Create New Sheet Each Quarter

Apr 21, 2014

Macro to run every 3 months, copy the workbook its in, save as a new name, and then delete all of the tabs in the workbook except for the "Menu" tab?

However, I get errors with my code below.

[Code] .....

View 7 Replies View Related

VBA Code To Create New Tabs In A Worksheet?

Sep 5, 2013

I don't know VBA code but i am familiar enough to know that a code can be written to separate out information into different tabs on a worksheet.

For example, I have a standard worksheet with say 20 columns going across and 20,000 rows going down. At each "name" change in row 1, I would like a macro to copy into a new tab. Basically the end result will have a tab for each name and the corresponding columns that go with the name.

View 2 Replies View Related

Code To Create Customer Number

Dec 14, 2007

I have the following formula copied down from A3 to A200 in a spreadsheet, which creates a unique standardized customer number for the each names listed in B3 to B200:

=IF(B3="","","S"&LEFT(B3,1)&"-"&TEXT(SUMPRODUCT(--(LEFT($B$3:B3,1)=LEFT(B3,1)))*10,"0000"))

Therefore, with the following names listed in B3 to B10:

UPS
FedEx
Fisher Price
Bell Canada
Grand & Toy
Rogers Cable
Dominion
Blue Jays

The respective customer numbers would be:

SU-0010
SF-0010
SF-0020
SB-0010
SG-0010
SR-0010
SD-0010
SB-0020

I would like these numbers to be static, so I would appreciate it very much if someone can give me a VBA code to replace the formula.

View 9 Replies View Related

Modify Code To Create Worksheets

Dec 12, 2008

I have this code that will create 3 worksheets with the name "New Name" and the # ( 1/2/3 ) ..

How would I change the code if I want 3 worksheets with different name?
Like Sheet1 then Sheet 2 then Sheet3 or any other name?

'To add worksheets and change name with one code
For i = 1 To 3 'Creates 3 worksheets
Set ws = Worksheets.Add
ws.Name = "New Name" & i
Next i

View 9 Replies View Related

Create New File & Name Via Macro Code

Sep 1, 2006

I have been trying to create a macro that creates a new workbook and prompts the user to enter a new filename for it. I'm getting stuck with the code.

View 3 Replies View Related

Code To Check For And Create New Folder

Sep 13, 2006

I have a macro that creates a spreadsheet on a weekly basis and have been using it for quite sometime. Due to the large number of spreadsheets I would like to incorporate some code that would create a folder every month. I would assume that the code would need to include a check routine to see if a folder for that month exsists.

View 3 Replies View Related

Create Event Procedure Via Code

Nov 15, 2006

I'm trying to add a worksheet event via code. It works OK when I run it on its own but I get a run-time error 9 subscript out of range error when I try to run it from another procedure.

Please find attached my 2 bits of code. Any help greatly appreciated.

This is the bit that works OK on its own but not when called from my other procedure

Sub Code_To_Write_Code()

'This writes code to the new sheet

Dim StartLine As Long
Dim SheetToAddCodeTo
SheetToAddCodeTo = ActiveSheet.CodeName

With ActiveWorkbook.VBProject.VBComponents(SheetToAddCodeTo).CodeModule
StartLine = .CreateEventProc("Calculate", "Worksheet") + 1 ..............

View 9 Replies View Related

Create Chart Automatically Via Code

Dec 19, 2006

to write a code that would create a chart automatically in another worksheet when a button is clicked.

I have attached the sample data that I am working on.
Each row in the sheet represent 3 coordinates, which are:
coordinate # 1 = start, 0
coordinate # 2 = centre, level
coordinate # 3 = end, 0

View 6 Replies View Related

Create Workbook With Macro Code Within

Jan 11, 2007

Right now I use a VBA-Code to create new worksheets in a workbook. my Question:
is it possible to attach code to the newly created Worksheet via vba-code??

View 3 Replies View Related

Code To Create Folder And Inside Subfolder

Mar 7, 2014

The code below is sending back an error. It's supposed to create a folder and a subfolder within it and if the folder already exists then a msg box should pop up saying that folder already exists.

[Code] ....

View 14 Replies View Related

Code To Create/modify/save Templates..

Dec 11, 2009

I have quite a few Excel templates that need to be modified by changing/adding information or sheets to them. Instead of working through the lot manually, it would be good if I could do the same through code. With my little knowledge I can do this for workbooks. Need to typical code snippets or suggestions? e.g. to open a template file, save it with the original name, etc. The usual Dim srcWB as workbook, Set srcWB= ABC.xls and Workbooks(ABC.xls). open do not seem to work with template files (ABC.xlt)

View 5 Replies View Related

How To Use Solver Add-in Then To Create Own Data Analysis Code

Nov 18, 2011

Is it easier to use the Solver Add-in than to create your own data analysis code? We are planning to design a scheduler for around 12 technicians in our contact center (doing phone support).

View 5 Replies View Related

VBA Code To Create Border Around Used Cells In Worksheet

Nov 20, 2013

I'm writing/recording a macro and would like some VBA code. I'm trying to make a border around all used cells in my sheet. When I use the macro there will be a variable number of used cells.

I'd like code that says:

If cell A2 has text then format cells A2:CL2 with an outline border Repeat for each row in the sheet until there there is no more text in column A.

Also, not sure if it matters, but I'm working on a mac.

View 9 Replies View Related

Macro Code To Create Line Graph

May 2, 2014

I have a macro code that will create line graph referring the data given in defined column A1-C4,

Code with Static column range:-

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet2'!$A$1:$C$4")
ActiveChart.ChartType = xlLineStacked

I tried to modify the above code, so it will refer undefined/dynamic data column, but getting an error during execution "Run Time Error - 424:" "Object required"

Code with Dynamic column range:-

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=ActiveSheet.Range("a1", _
ActiveSheet.Range("a1").End(xlDown).End(xlToRight)).Select
ActiveChart.ChartType = xlLineStacked

View 1 Replies View Related

Parse HTML Code, Create A Table

Mar 13, 2008

I have a function that locates a table on a webpage and pulls the html code into one cell in a worksheet. Basically we can call this one cell a text file. I need to parse through this text file (cell A1) to create a table. This text file only has info for 1 table, the table always has 12 columns, the rows are variable. I would like it to then write back this parsed text file back into excel as a table, say starting in cell A2 on the same worksheet.

View 9 Replies View Related

Code To Create Directories From Cell Values

Feb 26, 2009

I have a CSV file and would like to create directories on my hard drive and the names of those directories will need to be pulled out from this csv file: [B1:B86].

For example I have the following data in my csv file:

B1: folder1
B2: folder2
.
.
.

And would like to create folders on my hard drive so that folder names will be: folder1, folder2, ...

View 9 Replies View Related

Create CommandButton With Code & Position It On Sheet

Aug 25, 2008

How can I create a commandbutton with code & position it.

View 4 Replies View Related

VBA Macro Code To Create Multiple Charts

May 9, 2009

I need to create 63 charts from data which I have in two columns. I want to create multiple charts using one macro. For the first chart I want it to use cells K2:K80 as the x values, and M2:M80 as the y values. For the next chart I want it to use cells K81:K159 as the x values and M81:159 as the y values. For the next chart I want it to use cells K160:K238 as the x values and M160:M238 as the y values. I want to continue this, creating a chart for every 78 cells of data, all the way until the 63rd chart which uses K4900:K4978 as the x values and M4900:M4978 as the y values. I have created the following macro by " recording." This macro generates the first chart that I want:

Sub Macro5()
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$K$2:$K$80"
ActiveChart.SeriesCollection(1).Values = "=Sheet1!$M$2:$M$80"
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveWindow.SmallScroll Down:=-3
End Sub

How can I alter this macro to create all 63 charts?. It seems like there is an easy way to do this, but I don't use macros very much (at all).

View 2 Replies View Related

Create Dynamic Named Ranges With Code

Oct 13, 2007

I am employing code to label dynamic ranges that takes the form of,

ActiveWorkbook.Names.Add Name:="dms", RefersToR1C1:="=OFFSET(DMS!R10C5,0,0, COUNTA(DMS!C5),COUNTA(DMS!R10))"
'dms_j
ActiveWorkbook.Names.Add Name:="dms_j", RefersToR1C1:="=OFFSET(DMS!R11C10,0,0,MATCH("" * "",DMS!C10,-1),1)"
'dms_p
ActiveWorkbook.Names.Add Name:="dms_p", RefersToR1C1:="=OFFSET(DMS!R11C16,0,0,MATCH("" * "",DMS!C16,-1),1)"
'dms_r
ActiveWorkbook.Names.Add Name:="dms_r", RefersToR1C1:="=OFFSET(DMS!R11C18,0,0,MATCH("" * "",DMS!C18,-1),1)"
'dms_t
ActiveWorkbook.Names.Add Name:="dms_t", RefersToR1C1:="=OFFSET(DMS!R11C20,0,0,MATCH("" * "",DMS!C20,-1),1)"

The dynamic ranges are getting entered but the problem is that the range "dms" overshoots by six cells into blank cells at the bottom of the table, and the rest of the ranges overshoot by ten cells into blank cell area. I have deleted, cleared and destroyed everything around the table and re-sized the active area to no avail.

View 9 Replies View Related

Create UserForm With Controls Via Macro Code

Apr 20, 2008

This code create a userform on the workbook opening .I'm having problems with

1. Closeing the Visual basic window after this procedure runs
2. Being able to use the create userformstext box value in other procedures
3. how to delete the userform on close

Sub AddUserFormInputRequest()
Dim objVBProj As VBProject
Dim objVBComp As VBComponent
Dim objVBFrm As UserForm
Dim objChkBox As Object
Dim x As Integer
Dim strCode As String
Dim firstLine As Long, SecondLine As Long
Set objVBProj = Application.VBE.ActiveVBProject
Set objVBComp = objVBProj.VBComponents.Add(vbext_ct_MSForm)
With objVBComp
. Name = "InputRequest"
. Properties("Width") = 200
.Properties("Height") = 100......................

View 6 Replies View Related

Creating VBA Code That Uses Input From Users To Create A Calendar

Sep 18, 2012

Modify Macro3 and use the InputBox function twice so that Macro3 would ask the user for a particular month and a particular year; and then Macro3 uses these user’s inputs to create the calendar template for that month of the year. For example, if the user enters February for the month and 2012 for the year, Macro3 would create a new

VB:

Sheets("Template").Select
Sheets("Template").Copy After:=Sheets(1)
Sheets("Template (2)").Select
Sheets("Template (2)").Name = "January"

[Code]...

View 1 Replies View Related

VBA Code To Create SUMIF Function That Adds Comments?

Feb 28, 2013

how to create a function in VBA that works exactly as SUMIF, except it will also add a comment in the cell that references the formula, where the comment would be a list of the individual cells being added? Basically the comment would read "2+4+5+6" or "2,4,5,6" or something along that.

View 1 Replies View Related

To Create A Userform With A Textbox To Enter A Product Code

Jun 11, 2009

i would like to create a userform with a textbox to enter a product code , a command button to run VBA and a big text box to show output of VBA from Textbox 1 which i will connect to DB and pull back information relevant to that product code , is this possible i can create the userform no problem and already have code to pull back from DB into Excel ,

View 9 Replies View Related

Sort And Move Code, Create Equipment Lists`

Mar 5, 2007

I have been using Excel to create equipment lists for my work. The forum has helped me by creating a visual basic macro to create a 'final list' sheet compiled from the other source worksheets.

On my 'final list' sheet I need the first 10 rows to be left out of the macro’s so that I can add header information like job reference etc. I have tried altering the visual basic code by adjusting the row numbers in it so that it starts at row 10 but this keep's on producing error codes. This is due to my limited knowledege of what each line does.

I also wish to be able to paste new equipment into the final list sheet. When I do this at the moment the code does not recognise it is there and does not carry the information accross back into the source sheet. It is the same when I block select an area and delete the code does not recognise I have done this which is fustrating.

View 4 Replies View Related

Create A New Table That Displays The Information By Location Instead Of Code

Sep 7, 2007

I have a table of information with location codes as the column headers. Each location has from 1 to 6 codes associated with it.

I want to create a new table that displays the information by location instead of code, i.e. adding all of the codes for a location into one column for that location.

I'm hoping the example will make this clearer.

On the main page, I am trying to add in the wa column all of the columns in the raw page that have a code associated with wa as the header.

To make this more complex, I can't use vba on this one.

the only thing I've got so far is a very long, very complex formula that adds together numbers generated from index/matching each entry in the second table.

something like this.

(edit changing 1:1 to $1:$1)

=IF(VLOOKUP(B$1,lookup1,2,FALSE)=0,0,INDEX(sample.xls!main_table,ROW(),MATCH(VLOOKUP(B$1,lookup1,2,FALSE),raw!$1:$1,0)))+IF(VLOOKUP(B$1,lookup1,3,FALSE)=0,0,INDEX(sample.xls!main_table,ROW(),MATCH(VLOOKUP(B$1,lookup1,3,FALSE),raw!$1:$1,0)))+IF(VLOOKUP(B$1,lookup1,4,FALSE)=0,0,INDEX(sample.xls!main_table,ROW(),MATCH(VLOOKUP(B$1,lookup1,4,FALSE),raw!$1:$1,0)))+IF(VLOOKUP(B$1,lookup1,5,FALSE)=0,0,INDEX(sample.xls!main_table,ROW(),MATCH(VLOOKUP(B$1,lookup1,5,FALSE),raw!$1:$1,0)))+IF(VLOOKUP(B$1,lookup1,6,FALSE)=0,0,INDEX(sample.xls!main_table,ROW(),MATCH(VLOOKUP(B$1,lookup1,6,FALSE),raw!$1:$1,0)))


It works, but its horribly ugly, and if the number of locations goes higher (we could be looking at going to ten location codes for one of our locations) then I'll actually exeed the 1000 characters per formula limit!

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved