I am entering each Barcode data to an Excel File. But I want to keep the excel file minimized and data coming from Barcode reader automatically enter in that minimized Excel sheet. Is that possible using VBA Code or using any other method? I have tried some methods but didn't get what I need. So can anyone help me? Thanks in advance.
I have tried this code to set the file Active.
Sub test()
Dim wbName As Window
Set wbName = ActiveWorkbook.Windows(1)
'You can use Windows("[Workbook Name]") as well
wbName.Visible = False
wbName.Visible = True
End Sub
But this is not I need. I need automatically enter barcode details to Excel file when it is minimized.
Don't use 'ActiveWorkbook', but refer to the workbook with its actual name.
Something like
sub test()
dim wb as workbook
set wb = Workbooks("Book1.xslx")
'insert code
end sub
difficult to explain further as we don't have any reference on what you've tried so far.
Related
I have what it seems a small issue but cannot figure out how to solve.
So, every week I have to make a bunch of pivot tables to summarize raw data that I am getting. I figured, it would be much easier if I create all the tables into a Layout file and then just copy them to the new file and then change the source. And I wrote the following code:
Sub something()
Workbooks.Open Filename:="C:\\\\Layout.xlsx"
With Workbooks("Layout.xlsx")
.Sheets("Pivot").Copy Before:=ActiveWorkbook.Sheets("Main List")
.Close savechanges:=False
End With
End Sub
This code works just fine. The issue comes when I try to paste the code into my personal workbook and then run through VBA directly, rather than copying and pasting it every time.
What I could do is, to change the code from 'Activeworkbook' to the name of my new workbook but its name change every week, because it is based on a date.
I already tried to declare the name of the new workbook as a string and call it through that but that didn't work either.
Any ideas?
I would use workbook objects to keep the two files clear:
Sub something()
Dim wb_from As Workbook, wb_to As Workbook
Set wb_to = ActiveWorkbook
Workbooks.Open Filename:="C:\\\\Layout.xlsx"
Set wb_from = Workbooks("Layout.xlsx")
wb_from.Sheets("Pivot").Copy Before:=wb_to.Sheets("Main List")
wb_from.Close SaveChanges:=False
end sub
The macro is simple, but I have literaly NO IDEA what is going wrong! I've search everywhere for an answer but apparently no one else have had this same problem.
Here it is:
Sub Test()
Dim wkbkP As Workbook
Dim wkbk As Workbook
Set wkbkP = ThisWorkbook 'Principal workbook
Set wkbk = Workbooks.Open("C:\Users\wb\Desktop\source.xlsx")
MsgBox (wkbkP.Name)
MsgBox (wkbk.Name)
End Sub
The ideia of the original macro is to use this wkbk as source for some data that I will use in the wkbkP.
Anyway, I was testing to see if it was working, but apparently it's not "reading" or "understanding" this line
Set wkbk = Workbooks.Open("C:\Users\wb\Desktop\source.xlsx")
cause when I ask it to show wkbk's name in the MsgBox, it shows me the same name as the ThisWorkbook(and no, they are not the same file).
Thanks for helping!
I was trying to copy a text file and paste the data in excel using the following code. The code works satisfactorily to the extent copying and pasting data in the destination excel sheet, but additionally, opens up another excel file and sheet with the same name as the Text file, without being prompted anywhere in the code, and pastes the data there as well. This is not desirable. I don't want to split the data to columns or perform any other actions. It is a plain and simple copy and paste task. I have searched this and various other websites for an answer but failed to get one that appropriately addresses my problem. I am unable to figure out the flaw in the code, and therefore, seek your help. Any help would be most thankfully acknowledged.
Here is my code:
Sub CopyTextFile()
Set TxtFileName = Workbooks.Open("D:\Spares\Inventory\list_of_spares.txt")
TxtFileName.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks("Macro Test.xlsm").Activate
ActiveWorkbook.Sheets(1).Range("A1").Select
ActiveSheet.Paste
End Sub
You're getting the "extra file" because you are opening the text file in Excel (with the Workbooks.Open statement) and then copying the data from it.
Instead, open the file with a filesystemobject and read the data, then write it directly into your workbook:
Sub CopyTextFile()
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile : Set oFile = oFso.OpenTextFile("D:\Spares\Inventory\list_of_spares.txt", 1)
Dim sText
sText = oFile.ReadAll
oFile.Close
ThisWorkbook.Sheets(1).Range("A1").Value = sText
End Sub
See how that works for you?
I found a way to make your code work, you had to close the second workbook that was open, your code helped me thats why i am pointing out what was missing.
Sub CopyTextFile()
Set TxtFileName = Workbooks.Open("D:\Spares\Inventory\list_of_spares.txt")
TxtFileName.Sheets(1).Range("A1").CurrentRegion.Copy
Workbooks("Macro Test.xlsm").Activate
ActiveWorkbook.Sheets(1).Range("A1").Select
ActiveSheet.Paste
Workbooks(2).Close
End Sub
If you mention Workbooks(1).Close, it would close the first opened excel, here we want to close the second workbook hence Workbooks(2).Close
I found some excel vba code on this link which helped:https://www.excel-easy.com/vba/examples/close-open.html
enter image description hereI want to read data from two sheets whereas user will enter only date and after clicking button data will be populated in sheet.
Title Date
Enter Week Start Date "7/11/2016" (Button)
Name Project-ID Project Name Project Start Date Project End Date Sum Sum * 20
This is actual format of requirement.
in your line
Set objsheet = bjExcel.ActiveWorkbook.Worksheets("Config_InputAllocation_Weekly")
Set objsheet2 = objExcel.ActiveWorkbook.Worksheets("Config_Project")
you try to set both sheets active, which is not possible. There only be one active sheet. But you don't have to set them active. Use the following technique:
Dim objExcel as Object
Set objExcel = CreateObject("Excel.Application")
Dim myWb as workbook, myws1 as worksheet, myWs2 as worksheet
Set myWb = objExcel.Workbooks.Open("C:\Users\ABC\Documents\NSL\Automation Macro\NSL_DM_Tracker.xlsm")
Set myWs1 = myWb.Worksheets("Config_InputAllocation_Weekly")
Set myWs2 = myWb.Worksheets("Config_Project")
Then you can easily call the data from both sheets whether they are active or not:
Msgbox myWs1.Cells(1,1).value
Msgbox myWs2.Cells(1,1).value
Then the other problem is that you have defined an Sub searchdata() but it is not called within your CommandButton1_Click() Sub.
Moreover use Option Explicit setting, that is, put this line at the very top of your code:
Option Explicit
This will help you to debug your code.
Last but not least, in your Sub searchdata() no Worksheets are defined. Thus it will not know which one (myWs1 or myWs2) to use and when.
Good Luck,
Viktor
I have an SSIS package that creates and populates an excel sheet, it creates the sheet from a template file.
One of the issue's I had was that excel would change the format of the rows. I did a work around of inserting a header row and hiding it.
However I now need to script a VB task in SSIS that opens the excel sheet and deletes that specific row.
I've seen some articles online however have not been able to find any code I can try and replicate and my VB knowledge is very limited and I am really struggling.
SO far i've figured out how to delete the row.
Sub DeleteRow1()
Rows(4).Delete
End Sub
However i need to assign file strings and get the file to open and close as well...
after some discussions with a programmer here i managed to get some script to do exactly what I needed. Posting as an answer for anyone else out there who ever needs to do this in SSIS.
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
xlApp = New Excel.Application
xlApp.Workbooks.Open(Dts.Variables("NewFileName").Value.ToString)
xlSheet = xlApp.Workbooks(1).ActiveSheet
xlSheet.Rows(4).Delete()
xlApp.Workbooks(1).Save()
xlApp.Workbooks(1).Close()
xlSheet = Nothing
'
Dts.TaskResult = ScriptResults.Success
Uses a variable in SSIS to get the file name. This works perfectly although you do need to add Microsoft.Office.Interop.Excel Reference.
Sub deleteRow1()
Dim wkbk As Workbook
'You can also use an input box like this
' Dim wkbkname As Variant
' wkbkname = InputBox("Enter Workbook Name here.")
' Use wkbkname instead of wkbk then
Set wkbk = "Filename"
wkbk.Sheets(1).Rows(4).delete
End Sub
I hope that's what you were looking for.