Question
I need this problem to be solved in Excel VBA code.
Need full code in VBA. Please solve 1st Task : '5 or 0'. Please help me urgently with proper answer and coding solutions which I can run in Excel/vba. Thanks
Course: Programming languages Second laboratory work File and string processing Create a program that processes text file acc
0 0
Add a comment Improve this question Transcribed image text
Answer #1

For the 1st task Excel VBA code, please open a new Excel workbook and press "ALt+F11" to open Visual Basic Editor. In the windows insert a new module as shown below:

Microsoft Visual Basic for Applications - Book1 Eile Edit View Insert Format Debug Run Tools Add-Ins Window Help Project - VB

Create/define a new sub routine in the module where we'll write our code as shown below:

Microsoft Visual Basic for Applications - Book1 - [Module 1 (Code)] Eile Edit View Insert Format Debug Run Tools Add-ins Wind

Add the VBA code for the sub routine:

Sub trim_text()
  
'Define variables
Dim K As Integer, textFilePath As String, textline As String, i As Long
'i is the mapping of row number for output, we'll start with row 1
i = 1
'Get value of K from user by a prompt
K = InputBox("Please specify value of K:", "Value of K")
'Get file path of text file from user by a prompt
textFilePath = InputBox("Please specify text file path:", "Text File Path")
'Open textFilePath for input
Open textFilePath For Input As #1
'Run a Do loop till the end of lines in text file, basically read complete file line by line
Do Until EOF(1)
'Read one line and store in variable textline
Line Input #1, textline
'If condition to check if length of leading characters to be removed is greater than or equal to K
If (Len(textline) - K) >= 0 Then
'Store the output in Column A Row i
Range("A" + CStr(i)).Value = Right(textline, Len(textline) - K)
Else
'Store the output in Column A Row i
Range("A" + CStr(i)).Value = ""
End If
'Increment row count to store next value in next row on next iteration
i = i + 1
'End of loop code
Loop
'Close text file
Close #1
  
End Sub

Here's the screenshot:

Microsoft Visual Basic for Applications - Book1 - [Module 1 (Code)] Eile Edit View Insert Format Debug Run Tools Add-Ins Wind

Now we can run this subroutine to test our code. We can execute it by assigning it to a shortcut in Excel or for testing directly from VBE by pressing "F5" or Run button:

(General) Sub trim_text()

On execution it'll prompt user for value of K (leading characters to remove from each line) and full path of text file whose data will be processed. Below is my sample text file:

B Sample Text.txt X Enim curae vehicula non lobortis donec iaculis pharetra vestibulum himenaeos commodo praesent nulla 2 Eti

SampleText.txt

Enim curae vehicula non lobortis donec iaculis pharetra vestibulum himenaeos commodo praesent nulla
Etiam tellus purus
curabitur dolor tellus aliquet
Ultrices nam
iaculis tristique congue dolor ligula
Donec curabitur nam torquent risus
taciti, gravida condimentum congue gravida justo, bibendum habitant purus sagittis
Per suspendisse curabitur aptent porta tellus bibendum vestibulum ut donec tempor netus
Cubilia luctus sollicitudin gravida imperdiet dolor himenaeos quis
Est aenean aliquam tristique curae
donec vestibulum
Fermentum pulvinar fusce
euismod
porta eros egestas bibendum tempus
ipsum orci vulputate odio pharetra

On execution of the subroutine, you'll be shown prompts as shown in below screenshots:

First specify the value of K.

General EL Wrap Text Merge & Center - $ - % Insert Delete Fo - Conditional Format as Formatting Table Cell Styles Alignment N

Specify the text file path in next prompt (it'll need full path of the file, no quotes enclosing the path):

1 2 E 3 EX 2 E Insert Delete Format a Wrap Text General s Merge & Center - $ - % , Alignment al Number OO Conditional Format

Once you click OK, it'll read the text file line by line and remove leading K characters from each line. If number of characters in a line is less than K, all characters will be removed. Then the trimmed line will be copied to Excel sheet row by row. Here's the output of my code for SampleText.txt file shown earlier:

A1 X ✓ fica non lobortis donec iaculis pharetra vestibulum himenaeos commodo praesent nulla A B C D E F G H 1 la non lobortis

Please do note that this code doesn't implement any error checks, so incorrect input like specifying some string instead of a number for K or specifying incorrect file path will lead to run-time error. You can always re-run the code with correct inputs.

Add a comment
Know the answer?
Add Answer to:
I need this problem to be solved in Excel VBA code. Need full code in VBA....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ASAP. I need VBA code, for Excel please 5. Create text file with notepad. Each line...

    ASAP. I need VBA code, for Excel please 5. Create text file with notepad. Each line contains only one word. Open it and, if the word begins with capital letter, output it to column A, otherwise in column B.

  • JAVA PROGRAMMING File Name You can not just take the file and send it. When Polycarp...

    JAVA PROGRAMMING File Name You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed. Determine the...

  • java find and replace code pls help me We write code that can find and replace...

    java find and replace code pls help me We write code that can find and replace in a given text file. The code you write should take the parameters as command line arguments: java FindReplace -i <input file> -f "<find-string>" -r "<replace-string> -o <output file> *question mark(?) can be used instead of any character. "?al" string an be sal ,kal,val *In addition, a certain set of characters can be given in brackets.( "kng[a,b,f,d,s]ne" string an be kngane,hngbne,kangfne,kangdne,kangsne So, all you...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And...

    Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters.   For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • I need eclipse code for : Write a program that analyzes text written in the console...

    I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...

  • java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text...

    java programming Problem 2 (25 points) Counting Character Frequency. Write a program to analyze a text file (a novel or a report, or just a sequence of letters or symbols), by reading the file into a byte array, convert to a string, and then scan the string letter by letter to count the frequencies of all unique characters in the text, after that, the letters with frequencies greater than 0 and the frequencies are reported side by side. All members...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT