Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all 50070 articles
Browse latest View live

Need help setting up an Excel Loop thru a query table

$
0
0
What is the best way to set up a look thru an Excel query table? I have a worksheet of query data "SelectedData"

Need to loop thru each of these query table rows and for each row I come across add an entry into a row in worksheet "MainData"

I have the logic written to insert data into my "MainData" worksheet but I cannot find anything to show me how to loop thru my query table and process send the current query table row to my Insert_Rows_General subroutine.

Might be helpful if you saw my attached worksheet. I take the Prefix_Id then do a lookup in another WS to get a GL_Acct. I then take the GLAcct and 3 amounts and create an entry in the MainData Worksheet.

Entry in MainData table would be as follows
CurrentDate, Descr Glacct NetAmnt TaxAmnt, GrossAmnt

I use offsets in MainData table to determine in which columns I put the data.

Want to know how to properly code the loop thru this query table. Have too be careful that it is upward compatible when moving from 2007 to 2010 to 2013 etc.

Amy help would be greatly appreciated.
Attached Files

Running total VBA

$
0
0
I am creating a spreadsheet for inventory use. I want to have a running total in (1) cell based upon a new/different number being entered into a different cell.

Column B, Row 1 (This will be a new/different number entered every day - inventory in or out, so positive or negative number)
Column D, Row 1 (This will be a running total based on numbers inserted in previous 2 columns)

Basically what I have is a key inventory. So there is column A with key number, B should be keys IN, C should be keys OUT, or ideally B would be IN and OUT meaning, for inventory IN input a positive number and for inventory OUT input a negative number, and D Should be total.

Column E represents number of keys currently in the inventory. I was going to hide the current inventory column so all you see is IN/OUT and total.
So what I need is to be able to just come in and type in a number in the IN and/or OUT column, without having to add/subtract it with the number already in that column.

Bottom line, I’d like to be able to use IN and/or OUT columns to just type in numbers as they come and not have to worry about what’s already in those columns and get correct total number.

Can someone please help me with this?

Thanks!


Ok here it is. Attached worksheet shows
Column A - Key Numbers...No data value
Column B - Inventory IN
Column C - Inventory OUT
Column D - Total
Column E - Current Inventory(Starting point)

So the formula I used to get what i currently have is (=B2-C2+E2). This way whatever i input in columns C and C. totals out in D. But this way every time I want to add/subtract a number in B and C, i have to add to the number already in the column. I'd like to be able to type in a number in B and C as i go and still have a correct total. I wouldn't mind having just one column for in/out and use positive and negative numbers to differentiate inventory in or out.

I hope this makes sense. Thanks
Key Inventory - Test.xlsx

Copying all used rows into existing table on a separate worksheet - MACRO takes FOREVER!

$
0
0
I have a workbook with 3 worksheets: Worksheet 1 is a pivot table that pulls it's data from Table1, Worksheet 2 contains Table1, Worksheet 3 is a blank worksheet with 3 command buttons.

The blank worksheet is used for new data - someone adds the data pulled from our database, and then it needs to be added to Table1. Button1 on the blank worksheet re-formats the new data into what I need (moving columns around, concatenating certain cells, etc.), then Button2 on the blank worksheet is supposed to find all used rows on the sheet and copy it into Table1 on Worksheet2.

The problem I had was finding a way to add only the used rows to an existing table and then refreshing the pivot table, so the way I did it was not very elegant...

I'm still a novice a VBA, so I've piece-mealed my code together as best I could - and it works - it just takes FOREVER!! If anyone can offer any advice as to what's holding it up - or tell me a better way to do this - I would GREATLY appreciate it! I'm on a deadline for this project and I'm getting nervous!

Picture1.jpg

This is an excerpt from the macro attached to button 1 that selects all of the used rows on worksheet 3

Code:

Sub add_new_data()
'
' ADD_NEW_DATA Macro
'

' SELECTS ALL USED ROWS
  Range("A2:A" & LastRow).EntireRow.Select
   
End Sub

Function LastRow() As Long
  If WorksheetFunction.CountA(Cells) > 0 Then
          'Search for any entry, by searching backwards by Rows.
          LastRow = Cells.Find(What:="*", After:=[A1], _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
  End If
 
End Function

This is the macro attached to button 2 that's taking forever. First it converts Table1 (in Worksheet2, named "DATA") to a range. Then it removes all of the cell shading coloring, then it pastes the rows selected & copied from Worksheet3 (Named "NEW DATA"), re-formats all used cells on Worksheet2 back to a Table, does some extra stuff I need it to, then it refreshes the pivot table, deletes everything from Worksheet3 ("NEW DATA") so that it's blank again for the next time new data needs to be added, and then it does some other minor stuff I need it to do.

Code:

Sub copy_used_rows()

'turns off video card updating so that if you're doing a lot of formatting, it won't take as long
Application.ScreenUpdating = False

' Converts table to range

    Sheets("DATA").Select
    ActiveSheet.ListObjects("Table1").Unlist


'Removes any cell coloring
    Cells.Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
   
' Copies & pastes rows from New Data sheet
    Sheets("NEW DATA").Select
    Selection.Copy
    Sheets("DATA").Select
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown
   
' Formats all of the data as a table
    Range("A1").Resize(Cells.Find(What:="*", SearchOrder:=xlRows, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
      Cells.Find(What:="*", SearchOrder:=xlByColumns, _
      SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select
    ActiveSheet.ListObjects.Add(xlSrcRange, ActiveSheet.UsedRange, , xlYes).Name = _
        "Table1"
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium2"

' replace_LASS_with_CABS
    Columns("E:E").Select
    Selection.Replace What:="LASS", Replacement:="CABS", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

' replace_MSEPH_with_HPRO
    Selection.Replace What:="MSEPH", Replacement:="HPRO", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
       
' replace_MSTH_with_MSE
    Selection.Replace What:="MSTH", Replacement:="MSE", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

' REPLACE_WITH_SBCT

    Columns("E:E").Select
    Selection.Replace What:="SSCT", Replacement:="SBCT", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="BUWD", Replacement:="SBCT", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="BUS", Replacement:="SBCT", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


' REFRESHES PIVOT TABLE, THEN CLEARS OUT NEW DATA SHEET
    Sheets("Dean's Report Test 3.6 (2)").Select
    Range("E4").Select
    Application.CutCopyMode = False

    ActiveWorkbook.RefreshAll
    Sheets("NEW DATA").Select
    Selection.Delete Shift:=xlUp
   
' SORTS TABLE BY COLUMN A (NEEDED FOR VLOOKUP ON PIVOT TABLE SHEET TO WORK)
    ActiveWorkbook.Worksheets("DATA").ListObjects("Table1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("DATA").ListObjects("Table1").Sort.SortFields.Add _
        Key:=Range("Table1[[#All],[Crse]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("DATA").ListObjects("Table1").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
' CENTERS COLUMNS C & D
    Sheets("DATA").Select
    Columns("C:D").Select
    With Selection
        .HorizontalAlignment = xlCenter
    End With

' CONVERTS COLUMN C TO NUMBER FORMAT
    Range("C2").Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection
        Selection.NumberFormat = "General"
        .Value = .Value
    End With

' CONVERTS COLUMN D TO NUMBER FORMAT
    Range("D2").Select
    Range(Selection, Selection.End(xlDown)).Select

    With Selection
        Selection.NumberFormat = "General"
        .Value = .Value
    End With

   
End Sub

Position text boxes based on table of text box parameters

$
0
0
With the following code i'm trying to copy and paste text boxes (shapes) from one sheet to another, however i want to locate the text boxes based on values on another sheet, for example, below is an abbreviated list from sheet named "TextBoxes".

I also want to dynamically set the array to the text box names list in column A of the sheet named "TextBox", see attached image below for text box table

textboxtable.PNG

So the text boxes are copied from sheet named "Data", then pasted onto sheet named "Planning", then the location of the text box is defined by corresponding text box name in columns B thru E on the sheet named "TextBox"

It would appear that i'm having difficult setting the array, and i know that the method to set the text box location isn't correct either.

Any help would be appreciated:eek:

Code:

Sub TextBoxPosition()

Dim ws As Worksheet
Dim ws1 As Worksheet
Dim shSrc As Shape
Dim shDst As Shape
Dim lastrow As Integer


    Set ws = Worksheets("Planning")
    Set ws1 = Worksheets("Data")
    Set ws2 = Worksheets("TextBoxes")
   
    With ws2
        lastrow = Range("A" & .Rows.Count).End(xlUp).Row
    End With
     
    arrShapes = Array(ws2).Range("A2:A" & lastrow).Value)
   
    For i = LBound(arrShapes) To UBound(arrShapes)

        Set shSrc = ws1.Shapes(arrShapes(i))

        shSrc.Copy

        ws.Paste

        Set shDst = ws.Shapes(arrShapes(i))
     
        With shDst
            .Top = ws2.Offset(0, 1).Value
            .Left = ws2.Offset(0, 2).Value
            .Height = ws2.Offset(0, 3).Value
            .Width = ws2.Offset(0, 4).Value
        End With
       
    Next i

End Sub

Running total VBA

$
0
0
I am creating a spreadsheet for inventory use. I want to have a running total in (1) cell based upon a new/different number being entered into a different cell.

Column B, Row 1 (This will be a new/different number entered every day - inventory in or out, so positive or negative number)
Column D, Row 1 (This will be a running total based on numbers inserted in previous 2 columns)

Basically what I have is a key inventory. So there is column A with key number, B should be keys IN, C should be keys OUT, or ideally B would be IN and OUT meaning, for inventory IN input a positive number and for inventory OUT input a negative number, and D Should be total.

Column E represents number of keys currently in the inventory. I was going to hide the current inventory column so all you see is IN/OUT and total.
So what I need is to be able to just come in and type in a number in the IN and/or OUT column, without having to add/subtract it with the number already in that column.

Bottom line, I’d like to be able to use IN and/or OUT columns to just type in numbers as they come and not have to worry about what’s already in those columns and get correct total number.

Can someone please help me with this?

Thanks!


Ok here it is. Attached worksheet shows
Column A - Key Numbers...No data value
Column B - Inventory IN
Column C - Inventory OUT
Column D - Total
Column E - Current Inventory(Starting point)

So the formula I used to get what i currently have is (=B2-C2+E2). This way whatever i input in columns C and C. totals out in D. But this way every time I want to add/subtract a number in B and C, i have to add to the number already in the column. I'd like to be able to type in a number in B and C as i go and still have a correct total. I wouldn't mind having just one column for in/out and use positive and negative numbers to differentiate inventory in or out.

I hope this makes sense. Thanks
Attachment 310850

Pulling a value from a string

$
0
0
Hello!

I created a macro that opens a page as an excel sheet, grabs data from it, and inserts it into a cell.
My problem is that one of the cells has an abundance of data that I don't need.

The data that it puts into the cell looks like this: 0849940222,* Bill Myers ,* Thomas Nelson,*1997-10-02
The only thing I need is the first name that shows up.
The number at the beginning is always the same length, so I thought about using a trim function for that, but since names are not always the same length, I have no idea how to do the other end. Maybe get the value for commas and delete everything before the first and after the second...
I also don't know how to do that in VBA.
Any help would be appreciated!
Cheers!

(The data is the ISBN, Author, Publisher, and Date released)

Vba code to do a vlookup

$
0
0
Hi

I'm new to vba and need some help if possible, I am trying to look up data in column c into data on another tab but what i would also like to do is that where ever I am on the spreadsheet I still want to look up the data in column c. Any ideas on how to do this in vba will be very helpful, I have tried to do it however I can't seem to get it to work in any cell as the range keeps moving.thanks

Macro to read just the text in a cell containing text and numbers

$
0
0
Hi Guys I need to get a code that will just read the text in a cell that contains text and numbers example abc123 I want it to only read the abc as the numbers can change and cant write them all into my macro all the time.

Macro to filter multiple columns of data to multiple work sheets

$
0
0
Good Afternoon,

I am attempting to take information I input on a Master worksheet flow into multiple worksheets based on data in three different columns. I have attached an example worksheet.
I would like to be able to input information on one worksheet and have it flow to the appropriate worksheet.
I had assistance with something similar to this previously based upon on other criteria but alas my computer crashed and I lost the worksheet and everything attached literally two days later. I attempted to reuse the previous code but have this worksheet set up very differently that it just does not work. I need help!

It is not allowing me to attached an example. :(

Based on the attached essentially what I would like to have happen is:
USING THE SHEET (INITIAL SELECT) Main Columns Used would be CM, CX, DI, DT to flow to corresponding worksheet, meaning if CX=1, then it would flow to sheet one. yet at the same time I would like to exclude information associated with CX, DI, and DT as they would flow to their own sheets.
For instance
CM2=1, the information that would flow over to the corresponding sheet (1) would be CM, CL, BC, B, C, O, AK, BX, CN:CV
CX2=6, the information that would flow over to the corresponding sheet (6) would be CX, CW,BC, B, C, O, AK, BX, CY:DG
DI2=85, the information that would flow over to the corresponding sheet (85) would be DI, DH, BC, B, C, O, AK, BX, CY:DG
DT=47, the information that would flow over to the corresponding sheet (47) would be DT, DS, BC, B, C, O, AK, BX, DU; EC

Therefore if a person is requesting three separate things they are recorded on each things list, while only being calculated once (if that makes sense).
I’ve tried to write this code and have failed in so many ways. Your assistance is greatly appreciated!

[SOLVED] Select Range in VBA using Variables

$
0
0
Hi all--

I'm looking up to set up a range using just variables. This is what I thought it would be, but it's not working correctly. Any help would be appreciated!

Code:


    Dim Letter1 As Long
    Dim Letter2 As Long
    Dim Number1 As Long
    Dim Number2 As Long
   
    Letter1 = 1
    Letter2 = 4
    Number1 = 6
    Number2 = 6
   
    Dim RangeSelect As Range
   
    RangeSelect = Range(Cells(Letter1, Number1), Cells(Letter2, Number2))
    RangeSelect.Select

Exit For Loop on Error?

$
0
0
Hi everyone.

I have the following code which selects all constants from column 2 to 30, then cuts and pastes into column A:

Code:

For i = 2 To 100
    Columns(i).Select
    Selection.SpecialCells(xlCellTypeConstants, 23).Select
    Selection.Cut
    Range("A1").Select
    Selection.End(xlDown).Offset(1, 0).Select
    ActiveSheet.Paste
Next i

However, when the code does selection.special on totally blank columns, an error is generated. Is there a way to modify it so that it exits the loop if that error occurs?

Any help would be much appreciated.

Traspose data

$
0
0
Hi..


I have data in excel where the data from row need to bring into column
and the same name may not repeat and count the total

And it can be said as Transpose data from X to Y


I have attached sample file


Thanks
Attached Files

Storing individual digits to Array Cells

$
0
0
I am wondering how to accomplish storing individual digits of a large number to cells of an array. Basically, let's say you have 11!, and you want to store the answer's digits into individual cells.

I have 2 arrays set up; F(1 To 30) which takes care of the multiplication of each term, and C(1 To 30) which calculates the carries of each mult. and adds the result to the answer.

The reason I must do it this way is because something like 25! is too big to fit into a Long-integer location, and you'll get an error message.

So, if the answer of 11! is 39,916,800 , how would I separate the number so to be stored in array F(I) as:
F(0) = 0
F(1) = 0
F(2) = 8
F(3) = 6
F(4) = 1
F(5) = 9
F(6) = 9
F(7) = 3

Here's what I have so far, I am pretty sure it's completely incorrect, if you could just give me suggestions that would be great:


Code:

Dim F(1 To 30), C(1 To 30), N As Integer, Fact As Long, Length As Integer, space As Integer

Sub LargeFactorialCalc()
Open "F:\project.txt" For Output As #1

Fact = 1

N = InputBox("Enter the number you would like to take the factorial of: ")

For I = 1 To N

    Fact = Fact * I

    Call Split(Fact, F, N, C, I)
   
Next I

Print #1, Fact,
Close #1
End Sub


Sub Split(Fact, F, N, C, I)

Length = Len(Fact)

For I = 1 To N

F(I) = Val(Left(Fact, 1))
C(I + 1) = Val(Left(Fact, 2))
C(I + 2) = Val(Left(Fact, 3))

Next I

Emailing CheckBox Label Value

$
0
0
Hi Folks,

Quick question that I'm not sure whether or not is answerable. I have no problem emailing a copy of my userform's text boxes and combo boxes. The issue I am having is when it comes to check boxes. Is it possible to do one of the two following things in regards to emailing?

A) Have the label of a checkbox copied into email if the box is checked?

or

B) If I type the label name in the email coding, is it possible to have yes or no displayed based on whether or not the checkbox is checked?

If either one is possible, could you provide the coding assuming the checkboxes are named box1, box2, etc.?

Thank you!

[SOLVED] Macro to Header names into an array

$
0
0
Hello,

I need a Macro that copies Header names into a array.

I can not find or figure out a solution to this

Thanks

code for hide rows

$
0
0
hi guys

im looking for some simple code to hide rows based on this criteria

i want it to remove all rows if any cell in column b has no data in with the exception of 1

ie in cells b13:b27.
if cells b13 and b14 hold data i i want it to hide rows 15,16,17,18,19,20,21,22,23,24,2 and 26

is this possible

many thanks for your time

Excel Login FForm Help w/VBA

$
0
0
Hey, Looking for some help with the login form.

I have created a login form to use a worksheet calculator i made.

The loging form pops up when I open the excel file. everything works fine (enter the correct username and password, I get access to the work sheet. If i enter the incorrect password or username the worksheet closes)

The problem Is the pop up login form has a red X on the top right hand corner. If i click the X (close) then the login form closes andany user will have access to the worksheet rendering the login form useless.

Can any one here help me?

In my User Form -Under my "login" button I have coded below--

Code:

Private Sub CommandButton1_Click()
Dim username, password As String

username = TextBox1.Text
password = TextBox2.Text

If username = "Android" And password = "Phone" Then
    MsgBox "Correct", vbInformation
    Unload Me 'unload this form
   
Else
    MsgBox "Incorrect", vbCritical
   
    'now close the workbook
    ActiveWorkbook.Close
   
End If
End Sub

------

under - ThisWorkbook - in Workbook I have coded below for my popup userform

Code:

Private Sub Workbook_Open()
    'show the form when the workbook is open
    UserForm1.Show
   
End Sub


Any help is appreciated.
Attached Images

Moving a selection of cells when a ref. to that selection is placed in another cell.

$
0
0
Hi All

This is my first question I have been using some info. I have got from other peoples solutions but have not got the correct outcome for what I require. Would be greatful if someone could point me in the right direction as I am very new to excel/vba/macro programming but its starting to get me keen to try other stuff with excel.

I have attached a jpg showing the excel sheet which has all the data on it. What I am looking to do is when a number is inserted into the cells (B27:B33) blue circle (1) I want the cells next to the same number above to be copied into the cells next to the number put in. The example is number 14 and the cells (C19:Y19) have been copied into (C27:Y27). The sheet is going to be used to work out and calculate the hours worked and hours paid etc. I have filled the days in manually to give a better idea of whats required, I am also going to lock the sheet so only the number boxes are active.

I hope someone can help and am hoping this will give me a better idea of how to use programming to get better results from excel.

Thanks all in advance.

Ian E

Shift Hour Calculation.jpg

Change the content of a cell based on change the current date

$
0
0
All thanks to the excel forum Leaders , I want macro to sheet " gloos "
Change the content of cell E2 based on the current date .
Attached Files

Change Model to Category

$
0
0
Hello,

I am a beginner in VBA and thought I would try my hand at this simple (well I thought was simple) code, but unsure how to get the results I want.

1. In column A, I have models, in column B, I want to place either a "printer" if the model begins with Phaser or Laser, and if it begins with WorkCentre make it MFD. Also, if the Phaser has MFP on the end, make this an "MFD" and If Color 560 or Docucolor, make this "Out of Scope". How do I read the first part of the string in column A to do this, or do I need to make a second column with this Phaser and WorkCentre broken out?

I have this code attached, but only works if I have the word Phaser or WorkCentre.

Thanks
Attached Files
Viewing all 50070 articles
Browse latest View live